use-playback
Core hook for managing media playback state and controls
Installation
Install the hook
npx shadcn add @limeplay/use-playbackAdd Event & Action Bridge
Import the usePlaybackStates hook in your existing PlayerHooks component.
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
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.
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:
| Status | Description |
|---|---|
init | The media is initializing or has readyState 0 |
loading | The media is loading |
buffering | The media is buffering |
canplay | Enough data available to start playback |
canplaythrough | Enough data available to play through without buffering |
playing | The media is playing |
paused | The media is paused |
ended | The media has reached the end |
error | The media has encountered an error |
stopped | The 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 statestatus- Current media statusreadyState- Media ready state (0-4)canPlay,canPlayThrough- Playback readinesserror,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