Limeplay - Open Source Video Player UI ComponentsLimeplay

use-player

Hook for initializing and managing Shaka Player instance, loading assets, and preloading

Installation

Install the hook

npx shadcn add @limeplay/use-player

Add to PlayerHooks

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"

export const PlayerHooks = React.memo(() => {
  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

The usePlayer hook provides methods to load and preload assets.

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

export function LoadButton() {
  const { load, preload } = usePlayer({
    onError: (error, asset) => console.error("Error loading asset", asset, error),
    onLoad: async (asset, player, media) => {
        // Custom load logic if needed, or just:
        await player.load(asset.src)
    }
  })

  const handleLoad = () => {
    load({ id: "1", src: "https://..." })
  }

  return <button onClick={handleLoad}>Load Asset</button>
}

Understanding

The use-player hook (formerly use-shaka-player) handles the Shaka Player instance and asset loading lifecycle.

  • Initialization: usePlayerStates automatically loads the Shaka Player library and attaches it to the media element.
  • Loading: usePlayer provides a load function that uses a custom onLoad callback to handle asset loading.
  • Preloading: Supports preloading assets for instant playback.
  • Events: Synced with Zustand store (onPlayerReady, onBufferingChange, onError).

Debug Mode

When debug is enabled in the store, the hook loads the debug build of Shaka Player.

API Reference

usePlayerStates()

Initializes Shaka Player and attaches it to the media element. Also syncs player events like buffering to the store.

usePlayer(options)

Returns methods to interact with the player.

Options

Prop

Type

Returns

Prop

Type

Store State

The player instance and events are stored in the media store:

Prop

Type

On this page