Limeplay - Open Source Video Player UI ComponentsLimeplay

use-player

Core hook for managing media playback state and controls

Installation

Install the hook

npx shadcn add @limeplay/use-player

Add Event & Action Bridge

Import the usePlayerStates hook in your existing PlayerHooks component.

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

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

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

  return null
})

Add the Store States

lib/create-media-store.ts
import { createPlayerStore, PlayerStore } from "@/hooks/limeplay/use-player"


export type TypeMediaStore = PlayerStore & {} 

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

Example Usage

Use the usePlayer() hook to control playback in your components.

components/player/playback-control-button.tsx
import { usePlayer } from "@/hooks/limeplay/use-player"

export function PlaybackControlButton() {
  const { play, pause, togglePaused, restart } = usePlayer()

  return (
    <div>
      <button onClick={play}>Play</button>
      <button onClick={pause}>Pause</button>
      <button onClick={togglePaused}>Toggle</button>
      <button onClick={restart}>Restart</button>
    </div>
  )
}

Understanding

The use-player hook provides the foundation for all media playback functionality. It implements the Event & Action Bridge pattern:

  • Event Bridge: usePlayerStates() listens to native media events (play, pause, ended, buffering, etc.) and synchronizes state to the React store
  • Action Bridge: usePlayer() provides control functions (play(), pause(), togglePaused(), etc.) that manipulate the native media element

This hook is required for all other hooks and components as it provides the core media state management.

Media Status States

The hook tracks the following media status values:

StatusDescription
initThe media is initializing or has readyState 0
loadingThe media is loading
bufferingThe media is buffering
canplayEnough data available to start playback
canplaythroughEnough data available to play through without buffering
playingThe media is playing
pausedThe media is paused
endedThe media has reached the end
errorThe media has encountered an error
stoppedThe media has been stopped

API Reference

usePlayerStates()

Sets up event listeners for all media playback events. This hook should be called once in your PlayerHooks component. It automatically syncs the following state:

  • paused, ended, loop - Playback state
  • status - Current media status
  • readyState - Media ready state (0-4)
  • canPlay, canPlayThrough - Playback readiness
  • error, networkState - Error and network information

usePlayer()

Returns control functions for managing media playback.

Returns

PropertyTypeDescription
play() => voidStarts media playback
pause() => voidPauses media playback
togglePaused() => voidToggles between play and pause
setLoop(loop: boolean) => voidSets the loop state of the media
toggleLoop() => voidToggles the loop state
restart() => voidRestarts playback from the beginning

Store State

The player store provides the following state accessed via useMediaStore:

PropertyTypeDescription
idlebooleanWhether the player is in idle state
setIdle(idle: boolean) => voidSets the idle state
mediaRefReact.RefObject<HTMLMediaElement>Reference to the media element
setMediaRef(ref: React.RefObject<HTMLMediaElement>) => voidSets the media element reference
playershaka.Player | nullShaka Player instance
setPlayer(player: shaka.Player | null) => voidSets the Shaka Player instance
pausedbooleanWhether the media is paused
endedbooleanWhether the media has ended
loopbooleanWhether the media is looping
statusMediaStatusCurrent media status
readyStateMediaReadyStateMedia ready state (0-4)
canPlaybooleanWhether media can start playing
canPlayThroughbooleanWhether media can play through without buffering
errorMediaError | nullCurrent media error, if any
networkStatenumberNetwork state of the media