fn

useMediaControls

v0.0.14testeddemo

Reactive 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 playback
ts
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

ParameterTypeDescription
targetMaybeComputedElementRef<HTMLMediaElement | null | undefined>The media element (reactive ref, getter, or component instance)
options?UseMediaControlsOptionsSource/track configuration and a custom document

Returns

UseMediaControlsReturnReactive media state plus track and Picture-in-Picture controls
PropertyTypeDescription
currentTimeShallowRef<number>Current playback position in seconds. Writing seeks the media.
durationShallowRef<number>Total media duration in seconds (read-only mirror)
waitingShallowRef<boolean>Whether the media is buffering and waiting for more data
seekingShallowRef<boolean>Whether a seek operation is in progress
endedShallowRef<boolean>Whether playback has reached the end of the media
stalledShallowRef<boolean>Whether the browser is trying to fetch data but it is not forthcoming
bufferedShallowRef<Array<[number, number]>>Buffered time ranges as [start, end] second pairs
playingShallowRef<boolean>Whether the media is currently playing. Writing toggles play/pause.
rateShallowRef<number>Playback rate (1 is normal speed). Writing sets playbackRate.
playbackRateShallowRef<number>Alias of rate for API parity. Writing sets playbackRate.
volumeShallowRef<number>Audio volume in the range [0, 1]. Writing sets the element volume.
mutedShallowRef<boolean>Whether the media is muted. Writing mutes/unmutes the element.
tracksShallowRef<UseMediaTextTrack[]>Reactive snapshot of the element's text tracks
selectedTrackShallowRef<number>The id of the currently selected (showing) track, or -1 when none
enableTrack(track: number | UseMediaTextTrack, disableTracks?: boolean) => voidEnable a track (set to showing), optionally disabling all others first.
disableTrack(track?: number | UseMediaTextTrack) => voidDisable a track. With no argument, disables every track.
supportsPictureInPicturebooleanWhether the Picture-in-Picture API is available
togglePictureInPicture() => Promise<PictureInPictureWindow | void>Toggle Picture-in-Picture for a <video> element
isPictureInPictureShallowRef<boolean>Whether the element is currently in Picture-in-Picture mode
onSourceErrorMediaEventHookOn<Event>Register a callback fired when a <source> element errors
onPlaybackErrorMediaEventHookOn<unknown>Register a callback fired when playback errors (e.g. play() rejects)