Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useMediaControls.md
1---2category: Browser3---45# useMediaControls67Reactive media controls for both `audio` and `video` elements89## Usage1011### Basic Usage1213```vue14<script setup lang="ts">15import { useMediaControls } from '@vueuse/core'16import { onMounted, useTemplateRef } from 'vue'1718const video = useTemplateRef('video')19const { playing, currentTime, duration, volume } = useMediaControls(video, {20src: 'video.mp4',21})2223// Change initial media properties24onMounted(() => {25volume.value = 0.526currentTime.value = 6027})28</script>2930<template>31<video ref="video" />32<button @click="playing = !playing">33Play / Pause34</button>35<span>{{ currentTime }} / {{ duration }}</span>36</template>37```3839### Providing Captions, Subtitles, etc...4041You can provide captions, subtitles, etc in the `tracks` options of the42`useMediaControls` function. The function will return an array of tracks43along with two functions for controlling them, `enableTrack`, `disableTrack`, and `selectedTrack`.44Using these you can manage the currently selected track. `selectedTrack` will45be `-1` if there is no selected track.4647```vue48<script setup lang="ts">49import { useMediaControls } from '@vueuse/core'50import { useTemplateRef } from 'vue'5152const video = useTemplateRef('video')53const {54tracks,55enableTrack56} = useMediaControls(video, {57src: 'video.mp4',58tracks: [59{60default: true,61src: './subtitles.vtt',62kind: 'subtitles',63label: 'English',64srcLang: 'en',65},66]67})68</script>6970<template>71<video ref="video" />72<button v-for="track in tracks" :key="track.id" @click="enableTrack(track)">73{{ track.label }}74</button>75</template>76```7778## Type Declarations7980```ts81/**82* Many of the jsdoc definitions here are modified version of the83* documentation from MDN(https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement)84*/85export interface UseMediaSource {86/**87* The source url for the media88*/89src: string90/**91* The media codec type92*/93type?: string94/**95* Specifies the media query for the resource's intended media.96*/97media?: string98}99export interface UseMediaTextTrackSource {100/**101* Indicates that the track should be enabled unless the user's preferences indicate102* that another track is more appropriate103*/104default?: boolean105/**106* How the text track is meant to be used. If omitted the default kind is subtitles.107*/108kind: TextTrackKind109/**110* A user-readable title of the text track which is used by the browser111* when listing available text tracks.112*/113label: string114/**115* Address of the track (.vtt file). Must be a valid URL. This attribute116* must be specified and its URL value must have the same origin as the document117*/118src: string119/**120* Language of the track text data. It must be a valid BCP 47 language tag.121* If the kind attribute is set to subtitles, then srclang must be defined.122*/123srcLang: string124}125interface UseMediaControlsOptions extends ConfigurableDocument {126/**127* The source for the media, may either be a string, a `UseMediaSource` object, or a list128* of `UseMediaSource` objects.129*/130src?: MaybeRefOrGetter<string | UseMediaSource | UseMediaSource[]>131/**132* A list of text tracks for the media133*/134tracks?: MaybeRefOrGetter<UseMediaTextTrackSource[]>135}136export interface UseMediaTextTrack {137/**138* The index of the text track139*/140id: number141/**142* The text track label143*/144label: string145/**146* Language of the track text data. It must be a valid BCP 47 language tag.147* If the kind attribute is set to subtitles, then srclang must be defined.148*/149language: string150/**151* Specifies the display mode of the text track, either `disabled`,152* `hidden`, or `showing`153*/154mode: TextTrackMode155/**156* How the text track is meant to be used. If omitted the default kind is subtitles.157*/158kind: TextTrackKind159/**160* Indicates the track's in-band metadata track dispatch type.161*/162inBandMetadataTrackDispatchType: string163/**164* A list of text track cues165*/166cues: TextTrackCueList | null167/**168* A list of active text track cues169*/170activeCues: TextTrackCueList | null171}172export interface UseMediaControlsReturn {173currentTime: ShallowRef<number>174duration: ShallowRef<number>175waiting: ShallowRef<boolean>176seeking: ShallowRef<boolean>177ended: ShallowRef<boolean>178stalled: ShallowRef<boolean>179buffered: Ref<[number, number][]>180playing: ShallowRef<boolean>181rate: ShallowRef<number>182volume: ShallowRef<number>183muted: ShallowRef<boolean>184tracks: Ref<UseMediaTextTrack[]>185selectedTrack: ShallowRef<number>186enableTrack: (187track: number | UseMediaTextTrack,188disableTracks?: boolean,189) => void190disableTrack: (track?: number | UseMediaTextTrack) => void191supportsPictureInPicture: boolean192togglePictureInPicture: () => Promise<PictureInPictureWindow | void>193isPictureInPicture: ShallowRef<boolean>194onSourceError: EventHookOn<Event>195onPlaybackError: EventHookOn<Event>196}197export declare function useMediaControls(198target: MaybeRef<HTMLMediaElement | null | undefined>,199options?: UseMediaControlsOptions,200): UseMediaControlsReturn201```202