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

TextTrack

· 52CE 我爱测

The **`TextTrack`** interface of the WebVTT API represents a text track associated with a media element.

WebVTT

The TextTrack interface of the WebVTT API represents a text track associated with a media element.

An object of this type owns the list of VTTCue objects that will be displayed over the video at various points.

TextTrack objects can be added to a HTMLVideoElement or HTMLAudioElement element using the HTMLMediaElement.addTextTrack() method, which has the same effect as adding text tracks declaratively using track elements inside a video or audio element. The TextTrack objects are stored in a TextTrackList, which can be retrieved using the HTMLMediaElement.textTracks property.

Instance properties

This interface also inherits properties from EventTarget.

  • TextTrack.activeCues
    • : A TextTrackCueList object listing the currently active set of text track cues. Track cues are active if the current playback position of the media is between the cues' start and end times. Thus, for displayed cues such as captions or subtitles, the active cues are currently being displayed.
  • TextTrack.cues
    • : A TextTrackCueList which contains all of the track's cues.
  • TextTrack.id
    • : A string which identifies the track, if it has one. If it doesn't have an ID, then this value is an empty string (""). If the TextTrack is associated with a track element, then the track's ID matches the element's ID.
  • TextTrack.inBandMetadataTrackDispatchType
    • : Returns a string which indicates the track's in-band metadata track dispatch type.
  • TextTrack.kind
    • : Returns a string indicating what kind of text track the TextTrack describes. It must be one of the permitted values.
  • TextTrack.label
    • : A human-readable string which contains the text track's label, if one is present; otherwise, this is an empty string (""), in which case a custom label may need to be generated by your code using other attributes of the track, if the track's label needs to be exposed to the user.
  • TextTrack.language
    • : A string specifying the language in which the text track's contents is written. The value must be a valid BCP 47 language tag, for example "en-US" for United States English or "pt-BR" for Brazilian Portuguese.
  • TextTrack.mode
    • : A string specifying the track's current mode, which must be one of the permitted values. Changing this property's value changes the track's current mode to match. The default is disabled, unless the track element's default boolean attribute is set to true — in which case the default mode is showing.
  • TextTrack.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.

Instance methods

This interface also inherits methods from EventTarget.

[!NOTE] The TextTrackCue interface is an abstract class used as the parent for other cue interfaces such as VTTCue. Therefore, when adding or removing a cue you will be passing in one of the cue types that inherit from TextTrackCue.

  • TextTrack.addCue()
    • : Adds a cue (specified as a TextTrackCue object) to the track's list of cues.
  • TextTrack.removeCue()
    • : Removes a cue (specified as a TextTrackCue object) from the track's list of cues.

Events

  • cuechange
    • : Fired when cues are entered and exited. A given text cue appears when the cue is entered and disappears when the cue is exited. Also available via the oncuechange property.

Example

The following example adds a new TextTrack to a video, then sets it to display using TextTrack.mode.

let video = document.querySelector("video");
let track = video.addTextTrack("captions", "Captions", "en");
track.mode = "showing";

Specifications

Browser compatibility

See also

  • WebVTT
  • TextTrackCueList
  • VTTCue
  • track
  • HTMLTrackElement

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

资料来源与适用范围

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

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

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