Skip to content

usePlayerState

Copy as Markdown

const usePlayerState: <S>(source) => PlayerView<S>

The player’s state for a UI: the component renders again at each statechange, play and pause of the player.

S extends string = string

PlayerSource<S>

The ref given to <RupaPlayer ref>, or the player useRupaPlayer returns (see PlayerSource).

PlayerView<S>

{ player, state, paused } (see PlayerView).

import { useRef } from "react";
import type { Player } from "@rupamotion/player";
import { RupaPlayer, usePlayerState } from "@rupamotion/player/react";
export function Controls() {
const ref = useRef<Player>(null);
const { player, state, paused } = usePlayerState(ref);
return (
<>
<RupaPlayer ref={ref} src="/hero.rupaplay" markup="auto" />
<p>Playing: {state ?? "none"}</p>
<button disabled={!player} onClick={() => (paused ? player?.play() : player?.pause())}>
{paused ? "Play" : "Pause"}
</button>
</>
);
}