大网数据广告活动
Web 标准与前端开发

VideoTrack

· 52CE 我爱测

The **`VideoTrack`** interface represents a single video track from a video element.

HTML DOM

The VideoTrack interface represents a single video track from a video element.

The most common use for accessing a VideoTrack object is to toggle its VideoTrack.selected property in order to make it the active video track for its video element.

Instance properties

  • VideoTrack.selected
    • : A Boolean value which controls whether or not the video track is active. Only a single video track can be active at any given time, so setting this property to true for one track while another track is active will make that other track inactive.
  • VideoTrack.id
    • : A string which uniquely identifies the track within the media. This ID can be used to locate a specific track within a video track list by calling VideoTrackList.getTrackById(). The ID can also be used as the fragment part of the URL if the media supports seeking by media fragment per the Media Fragments URI specification.
  • VideoTrack.kind
    • : A string specifying the category into which the track falls. For example, the main video track would have a kind of "main".
  • VideoTrack.label
    • : A string providing a human-readable label for the track. For example, a track whose kind is "sign" might have a label of "A sign-language interpretation". This string is empty if no label is provided.
  • VideoTrack.language
    • : A string specifying the video track's primary language, or an empty string if unknown. The language is specified as a BCP 47 language tag such as "en-US" or "pt-BR".
  • VideoTrack.sourceBuffer
    • : The SourceBuffer that created the track. Returns null if the track was not created by a SourceBuffer or the SourceBuffer has been removed from the MediaSource.sourceBuffers attribute of its parent media source.

Usage notes

To get a VideoTrack for a given media element, use the element's HTMLMediaElement.videoTracks property, which returns a VideoTrackList object from which you can get the individual tracks contained in the media:

const el = document.querySelector("video");
const tracks = el.videoTracks;

You can then access the media's individual tracks using either array syntax or functions such as Array.forEach.

This first example gets the first video track on the media:

const firstTrack = tracks[0];

The next example scans through all of the media's video tracks, activating the first video track that is in the user's preferred language (taken from a variable userLanguage).

for (const track of tracks) {
  if (track.language === userLanguage) {
    track.selected = true;
    break;
  }
}

The VideoTrack.language is specified as a valid BCP 47 language tag, for example "en-US" for US English.

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。