Limeplay - Open Source Video Player UI ComponentsLimeplay

use-playback

Core hook for managing media playback state and controls

Installation

Install the hook

npx shadcn add @limeplay/use-playback

Add Event & Action Bridge

Import the usePlaybackStates hook in your existing PlayerHooks component.

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

import { usePlaybackStates } from "@/hooks/limeplay/use-playback"
import { usePlayerStates } from "@/hooks/limeplay/use-player"

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

  return null
})

Add the Store States

lib/create-media-store.ts
import { createPlaybackStore, PlaybackStore } from "@/hooks/limeplay/use-playback"


export type TypeMediaStore = PlaybackStore &
  {}

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

Example Usage

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

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

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

  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-playback hook provides the foundation for all media playback functionality. It implements the Event & Action Bridge pattern:

  • Event Bridge: usePlaybackStates() listens to native media events (play, pause, ended, buffering, etc.) and synchronizes state to the React store
  • Action Bridge: usePlayback() 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

usePlaybackStates()

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

usePlayback()

Returns control functions for managing media playback.

Returns

Prop

Type

Store State

The playback store provides the following state accessed via useMediaStore:

Prop

Type

On this page