fn
useMediaControls
v0.0.14testeddemoReactive controls and state for an <audio>/<video> element:
play/pause, seeking, duration, buffered ranges, volume, mute, rate, text tracks,
and Picture-in-Picture. Source and track injection are handled for you, and all
DOM listeners attach passively with automatic cleanup. SSR-safe.
Examples
ts
const video = useTemplateRef<HTMLVideoElement>('video');
const { playing, currentTime, duration, volume } = useMediaControls(video, {
src: 'https://example.com/clip.mp4',
});
playing.value = true; // start playbackts
const { tracks, enableTrack, togglePictureInPicture } = useMediaControls(video, {
src: 'video.mp4',
tracks: [{ default: true, src: 'en.vtt', srcLang: 'en', label: 'English', kind: 'subtitles' }],
});Demo
Loading demo…
Signature
ts
export function useMediaControls(
target: MaybeComputedElementRef<HTMLMediaElement | null | undefined>,
options: UseMediaControlsOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
target | MaybeComputedElementRef<HTMLMediaElement | null | undefined> | The media element (reactive ref, getter, or component instance) |
options? | UseMediaControlsOptions | Source/track configuration and a custom document |
Returns
UseMediaControlsReturnReactive media state plus track and Picture-in-Picture controls| Property | Type | Description |
|---|---|---|
currentTime | ShallowRef<number> | Current playback position in seconds. Writing seeks the media. |
duration | ShallowRef<number> | Total media duration in seconds (read-only mirror) |
waiting | ShallowRef<boolean> | Whether the media is buffering and waiting for more data |
seeking | ShallowRef<boolean> | Whether a seek operation is in progress |
ended | ShallowRef<boolean> | Whether playback has reached the end of the media |
stalled | ShallowRef<boolean> | Whether the browser is trying to fetch data but it is not forthcoming |
buffered | ShallowRef<Array<[number, number]>> | Buffered time ranges as [start, end] second pairs |
playing | ShallowRef<boolean> | Whether the media is currently playing. Writing toggles play/pause. |
rate | ShallowRef<number> | Playback rate (1 is normal speed). Writing sets playbackRate. |
playbackRate | ShallowRef<number> | Alias of rate for API parity. Writing sets playbackRate. |
volume | ShallowRef<number> | Audio volume in the range [0, 1]. Writing sets the element volume. |
muted | ShallowRef<boolean> | Whether the media is muted. Writing mutes/unmutes the element. |
tracks | ShallowRef<UseMediaTextTrack[]> | Reactive snapshot of the element's text tracks |
selectedTrack | ShallowRef<number> | The id of the currently selected (showing) track, or -1 when none |
enableTrack | (track: number | UseMediaTextTrack, disableTracks?: boolean) => void | Enable a track (set to showing), optionally disabling all others first. |
disableTrack | (track?: number | UseMediaTextTrack) => void | Disable a track. With no argument, disables every track. |
supportsPictureInPicture | boolean | Whether the Picture-in-Picture API is available |
togglePictureInPicture | () => Promise<PictureInPictureWindow | void> | Toggle Picture-in-Picture for a <video> element |
isPictureInPicture | ShallowRef<boolean> | Whether the element is currently in Picture-in-Picture mode |
onSourceError | MediaEventHookOn<Event> | Register a callback fired when a <source> element errors |
onPlaybackError | MediaEventHookOn<unknown> | Register a callback fired when playback errors (e.g. play() rejects) |