Limeplay - Open Source Video Player UI ComponentsLimeplay

use-captions

Hook for managing captions and text tracks in the media player

Installation

npx shadcn add @limeplay/use-captions

Usage

Basic Setup

Import the useCaptionsStates hook in your existing PlayerHooks component to enable captions functionality.

components/limeplay/player-hooks.tsx
import React from "react"

import { useCaptionsStates } from "@/hooks/limeplay/use-captions"
import { useShakaPlayer } from "@/hooks/limeplay/use-shaka-player"
import { usePlayerStates } from "@/hooks/limeplay/use-player"

export const PlayerHooks = React.memo(() => {
  useShakaPlayer()
  usePlayerStates()
  useCaptionsStates() 

  return null
})

Store Integration

Add the captions store to your media store configuration.

lib/create-media-store.ts
import { create } from "zustand"

import {
  createCaptionsStore,
  CaptionsStore,
} from "@/hooks/limeplay/use-captions"
import {
  createPlayerStore,
  PlayerStore,
} from "@/hooks/limeplay/use-player"
import {
  createVolumeStore,
  VolumeStore,
} from "@/hooks/limeplay/use-volume"

export type TypeMediaStore = PlayerStore &
  VolumeStore &
  CaptionsStore & {} 

export function createMediaStore(initProps?: Partial<CreateMediaStoreProps>) {
  const mediaStore = create<TypeMediaStore>()((...etc) => ({
    ...createPlayerStore(...etc),
    ...createVolumeStore(...etc),
    ...createCaptionsStore(...etc),
    ...initProps,
  }))
  return mediaStore
}

API Reference

useCaptionsStates()

Sets up event listeners for text track changes and visibility updates. This hook should be called once in your PlayerHooks component.

useCaptions()

Returns captions control functions for managing text tracks.

Returns

PropertyTypeDescription
toggleCaptionVisibility() => voidToggles the visibility of captions. Automatically selects a default track if none is active.

Store State

The captions store provides the following state:

PropertyTypeDescription
activeTextTrackshaka.extern.TextTrack | nullCurrently active text track
textTracksshaka.extern.TextTrack[] | undefinedAvailable text tracks
textTrackVisiblebooleanWhether captions are currently visible
textTrackContainerElementHTMLDivElement | nullContainer element for text track rendering
setTextTrackContainerElement(element: HTMLDivElement | null) => voidSets the container element for text tracks

Understanding

The use-captions hook provides comprehensive text track management for Shaka Player. It automatically:

  • Detects available text tracks when media loads
  • Manages text track visibility state
  • Provides automatic track selection based on device language
  • Handles container element setup for proper text rendering

The hook integrates seamlessly with Shaka Player's text track system and provides a clean API for caption controls in your player interface.