---
category: Browser
---
# useMediaControls
Reactive media controls for both `audio` and `video` elements
## Usage
### Basic Usage
```vue
{{ currentTime }} / {{ duration }}
```
### Providing Captions, Subtitles, etc...
You can provide captions, subtitles, etc in the `tracks` options of the
`useMediaControls` function. The function will return an array of tracks
along with two functions for controlling them, `enableTrack`, `disableTrack`, and `selectedTrack`.
Using these you can manage the currently selected track. `selectedTrack` will
be `-1` if there is no selected track.
```vue
```
## Type Declarations
```ts
/**
* Many of the jsdoc definitions here are modified version of the
* documentation from MDN(https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement)
*/
export interface UseMediaSource {
/**
* The source url for the media
*/
src: string;
/**
* The media codec type
*/
type?: string;
/**
* Specifies the media query for the resource's intended media.
*/
media?: string;
}
export interface UseMediaTextTrackSource {
/**
* Indicates that the track should be enabled unless the user's preferences indicate
* that another track is more appropriate
*/
default?: boolean;
/**
* How the text track is meant to be used. If omitted the default kind is subtitles.
*/
kind: TextTrackKind;
/**
* A user-readable title of the text track which is used by the browser
* when listing available text tracks.
*/
label: string;
/**
* Address of the track (.vtt file). Must be a valid URL. This attribute
* must be specified and its URL value must have the same origin as the document
*/
src: string;
/**
* Language of the track text data. It must be a valid BCP 47 language tag.
* If the kind attribute is set to subtitles, then srclang must be defined.
*/
srcLang: string;
}
interface UseMediaControlsOptions extends ConfigurableDocument {
/**
* The source for the media, may either be a string, a `UseMediaSource` object, or a list
* of `UseMediaSource` objects.
*/
src?: MaybeRefOrGetter;
/**
* A list of text tracks for the media
*/
tracks?: MaybeRefOrGetter;
}
export interface UseMediaTextTrack {
/**
* The index of the text track
*/
id: number;
/**
* The text track label
*/
label: string;
/**
* Language of the track text data. It must be a valid BCP 47 language tag.
* If the kind attribute is set to subtitles, then srclang must be defined.
*/
language: string;
/**
* Specifies the display mode of the text track, either `disabled`,
* `hidden`, or `showing`
*/
mode: TextTrackMode;
/**
* How the text track is meant to be used. If omitted the default kind is subtitles.
*/
kind: TextTrackKind;
/**
* Indicates the track's in-band metadata track dispatch type.
*/
inBandMetadataTrackDispatchType: string;
/**
* A list of text track cues
*/
cues: TextTrackCueList | null;
/**
* A list of active text track cues
*/
activeCues: TextTrackCueList | null;
}
export declare function useMediaControls(
target: MaybeRef,
options?: UseMediaControlsOptions,
): {
currentTime: ShallowRef;
duration: ShallowRef;
waiting: ShallowRef;
seeking: ShallowRef;
ended: ShallowRef;
stalled: ShallowRef;
buffered: Ref<[number, number][], [number, number][]>;
playing: ShallowRef;
rate: ShallowRef;
volume: ShallowRef;
muted: ShallowRef;
tracks: Ref<
{
id: number;
label: string;
language: string;
mode: TextTrackMode;
kind: TextTrackKind;
inBandMetadataTrackDispatchType: string;
cues: {
[x: number]: {
endTime: number;
id: string;
onenter: ((this: TextTrackCue, ev: Event) => any) | null;
onexit: ((this: TextTrackCue, ev: Event) => any) | null;
pauseOnExit: boolean;
startTime: number;
readonly track: {
readonly activeCues: /*elided*/ any | null;
readonly cues: /*elided*/ any | null;
readonly id: string;
readonly inBandMetadataTrackDispatchType: string;
readonly kind: TextTrackKind;
readonly label: string;
readonly language: string;
mode: TextTrackMode;
oncuechange: ((this: TextTrack, ev: Event) => any) | null;
addCue: (cue: TextTrackCue) => void;
removeCue: (cue: TextTrackCue) => void;
addEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
} | null;
addEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
};
readonly length: number;
getCueById: (id: string) => TextTrackCue | null;
[Symbol.iterator]: () => ArrayIterator;
} | null;
activeCues: {
[x: number]: {
endTime: number;
id: string;
onenter: ((this: TextTrackCue, ev: Event) => any) | null;
onexit: ((this: TextTrackCue, ev: Event) => any) | null;
pauseOnExit: boolean;
startTime: number;
readonly track: {
readonly activeCues: /*elided*/ any | null;
readonly cues: /*elided*/ any | null;
readonly id: string;
readonly inBandMetadataTrackDispatchType: string;
readonly kind: TextTrackKind;
readonly label: string;
readonly language: string;
mode: TextTrackMode;
oncuechange: ((this: TextTrack, ev: Event) => any) | null;
addCue: (cue: TextTrackCue) => void;
removeCue: (cue: TextTrackCue) => void;
addEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
} | null;
addEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
};
readonly length: number;
getCueById: (id: string) => TextTrackCue | null;
[Symbol.iterator]: () => ArrayIterator;
} | null;
}[],
| UseMediaTextTrack[]
| {
id: number;
label: string;
language: string;
mode: TextTrackMode;
kind: TextTrackKind;
inBandMetadataTrackDispatchType: string;
cues: {
[x: number]: {
endTime: number;
id: string;
onenter: ((this: TextTrackCue, ev: Event) => any) | null;
onexit: ((this: TextTrackCue, ev: Event) => any) | null;
pauseOnExit: boolean;
startTime: number;
readonly track: {
readonly activeCues: /*elided*/ any | null;
readonly cues: /*elided*/ any | null;
readonly id: string;
readonly inBandMetadataTrackDispatchType: string;
readonly kind: TextTrackKind;
readonly label: string;
readonly language: string;
mode: TextTrackMode;
oncuechange: ((this: TextTrack, ev: Event) => any) | null;
addCue: (cue: TextTrackCue) => void;
removeCue: (cue: TextTrackCue) => void;
addEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
} | null;
addEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
};
readonly length: number;
getCueById: (id: string) => TextTrackCue | null;
[Symbol.iterator]: () => ArrayIterator;
} | null;
activeCues: {
[x: number]: {
endTime: number;
id: string;
onenter: ((this: TextTrackCue, ev: Event) => any) | null;
onexit: ((this: TextTrackCue, ev: Event) => any) | null;
pauseOnExit: boolean;
startTime: number;
readonly track: {
readonly activeCues: /*elided*/ any | null;
readonly cues: /*elided*/ any | null;
readonly id: string;
readonly inBandMetadataTrackDispatchType: string;
readonly kind: TextTrackKind;
readonly label: string;
readonly language: string;
mode: TextTrackMode;
oncuechange: ((this: TextTrack, ev: Event) => any) | null;
addCue: (cue: TextTrackCue) => void;
removeCue: (cue: TextTrackCue) => void;
addEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrack, ev: TextTrackEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
} | null;
addEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | AddEventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
};
removeEventListener: {
(
type: K,
listener: (this: TextTrackCue, ev: TextTrackCueEventMap[K]) => any,
options?: boolean | EventListenerOptions,
): void;
(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
): void;
};
dispatchEvent: {
(event: Event): boolean;
(event: Event): boolean;
};
};
readonly length: number;
getCueById: (id: string) => TextTrackCue | null;
[Symbol.iterator]: () => ArrayIterator;
} | null;
}[]
>;
selectedTrack: ShallowRef;
enableTrack: (track: number | UseMediaTextTrack, disableTracks?: boolean) => void;
disableTrack: (track?: number | UseMediaTextTrack) => void;
supportsPictureInPicture: boolean | undefined;
togglePictureInPicture: () => Promise;
isPictureInPicture: ShallowRef;
onSourceError: EventHookOn;
onPlaybackError: EventHookOn;
};
export type UseMediaControlsReturn = ReturnType;
```