Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLMediaElement

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

HTMLMediaElement

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

* Some parts of this feature may have varying levels of support.

HTML 媒体元素接口在属性和方法中添加了HTML 元素来支持基础的媒体相关的能力,就像音频和视频一样。HTML 视频元素HTML 音频元素元素都继承自此接口。

must be a string

特性

从父级HTMLElementElementNodeEventTarget 继承属性。

名称类型描述
audioTracksAudioTrackList

表示在该元素中包含的AudioTrack对象列表

autoplayBoolean

表示autoplay 的 HTML 属性,表明在视频加载可用时是否不中断地自动播放资源

buffered只读TimeRangesbuffered属性会告诉浏览器哪一部分的媒体已经被下载(如果浏览器支持的话),按照标准会返回一个TimeRanges对象
controllerMediaController

返回当前媒体控制器的 MediaController 对象,如果没有连接就返回 null

controlsBoolean 映射在 HTML 标签。controls 属性控制是否显示用户播放界面的控制 HTML
crossOriginDOMString 一个表示媒体元素CORS 设置DOMString。从CORS settings attributes 查看更多详情
currentSrc只读DOMStringDOMString表示媒体文件的绝对 URL。如果networkStateEMPTY,那么值为空字符串。
currentTimedouble当前播放时间,单位为秒。为其赋值将会使媒体跳到一个新的时间。
defaultMutedBoolean 映射在 HTML 标签上。muted 属性表示媒体声音被播放时是否应该被静音。这个属性不能动态设置静音/不静音,如果希望设置静音/不静音,请使用muted 属性
defaultPlaybackRatedouble 控制媒体的播放速度。1.0 表示正常的播放速度,如果值小于 1.0,则播放速度会比”正常速度“慢,如果值大于 1.0,则播放速度会比”正常速度“快。0.0 是一个无效的值,并且会抛出NOT_SUPPORTED_ERR 错误。
duration只读double 媒体以秒为单位的总长度时间,如果媒体不可用,则为 0. 如果媒体可用,但时间长度未知,值为 NAN. 如果媒体是以 stream 形式传输并且没有预定长度,则值为 Inf。
ended只读Boolean表示媒体是否已经播放完毕。
error只读MediaErrorMediaError 对象表示最近的错误,如果没有错误则值为null
initialTime只读非标准已弃用double初始播放位置(以秒为单位)。
loopBoolean 会映射在 HTML 标签loop 属性 , 决定该媒体是否循环播放。
mediaGroupDOMString 反映在 HTMLmediagroup 标签上。 表示元素所归属的分组,同一组的媒体元素会共享同一个控制器(controller)。
mediaKeys实验性只读MediaKeys Returns a reference to theMediaKeys interface, which is a set of keys that an associated HTMLMediaElement can use for decription of media data during playback.
mozAudioChannelType非标准DOMString Can be used to set the audio channel that the sound coming from an<audio> or<video> element will play in, on a Firefox OS device. SeeUsing the AudioChannels API for more details.
mozChannels只读非标准long声道数 (比如2 是立体声).
mozFrameBufferLength非标准long

Indicates the number of samples that will be returned in the framebuffer of eachMozAudioAvailable event. This number is a total for all channels, and by default is set to be the number of channels * 1024 (e.g., 2 channels * 1024 samples = 2048 total).

ThemozFrameBufferLength property can be set to a new value, for lower latency, or larger amounts of data, etc. The size givenmust be a number between 512 and 16384. Using any other size results in an exception being thrown. The best time to set a new length is after theloadedmetadata event fires, when the audio info is known, but before the audio has started orMozAudioAvailable events have begun firing.

mozSampleRate只读非标准long 播放内容的采样率(每秒采样次数)。比如,44100 就是一张 CD 的采样率。
mozSrcObject非标准MediaStream Lets you set or get theMedia Stream to be played or being played.
mutedBoolean静音时为 true ,否则是false .
networkStateunsigned short 获取媒体时的网络状态
常量定义描述
NETWORK_EMPTY0 还没数据。readyStateHAVE_NOTHING.
NETWORK_IDLE1
NETWORK_LOADING2正在加载。
NETWORK_NO_SOURCE3
paused只读Boolean指示媒体元素是否被暂停。
playbackRatedouble

The current rate at which the media is being played back. This is used to implement user controls for fast forward, slow motion, and so forth. The normal playback rate is multiplied by this value to obtain the current rate, so a value of 1.0 indicates normal speed.

If theplaybackRate is negative, the media is played backwards.
The audio is muted when the media plays backwards or if the fast forward or slow motion is outside a useful range (E.g. Gecko mute the sound outside the range0.25 and5.0).

The pitch of the audio is corrected by default and is the same for every speed. Some browsers implement the non-standardpreservespitch property to control this.

played只读TimeRanges媒体可被播放的范围。
preloadDOMString Reflects thepreload HTML attribute, indicating what data should be preloaded, if any. Possible values are:none,metadata,auto. Seepreload attribute documentation for details.
readyState只读unsigned short The readiness state of the media.
ConstantValueDescription
HAVE_NOTHING0No information is available about the media resource.
HAVE_METADATA1 Enough of the media resource has been retrieved that the metadata attributes are initialized. Seeking will no longer raise an exception.
HAVE_CURRENT_DATA2 Data is available for the current playback position, but not enough to actually play more than one frame.
HAVE_FUTURE_DATA3 Data for the current playback position as well as for at least a little bit of time into the future is available (in other words, at least two frames of video, for example).
HAVE_ENOUGH_DATA4 Enough data is available—and the download rate is high enough—that the media can be played through to the end without interruption.
seekable只读TimeRanges The time ranges that the user is able to seek to, if any.
seeking只读Boolean Indicates whether the media is in the process of seeking to a new position.
sinkId只读实验性DOMString The unique ID of the audio device delivering output, or an empty string if it is using the user agent default. This ID should be one of theMediaDeviceInfo.deviceid values returned fromMediaDevices.enumeratedDevices(),id-multimedia, orid-communications.
srcDOMString Reflects thesrc HTML attribute, containing the URL of a media resource to use. Gecko implements a similar functionality for streams:mozSrcObject.
textTracksTextTrackList Represents the list ofTextTrack objects contained in the element.
videoTracksVideoTrackList Represents the list ofVideoTrack objects contained in the element.

Note: Yet Gecko supports only single track playback, and the parsing of tracks' metadata is only available for media with Ogg container foramt.

volumedouble表示音频的音量。值从 0.0(静音)到 1.0(最大音量)。

Methods

Inherits methods from its parent,HTMLElement.

Name & ArgumentsReturnDescription
canPlayType(inDOMString type)DOMString
  • probably: if the specified type appears to be playable.
  • maybe: if it's impossible to tell whether the type is playable without playing it.
  • Theempty string: if the specified type definitely cannot be played.
Determines whether the specified media type can be played back.

Note: PreviouslycanPlayType('video/webm') returned'probably'. Starting with Gecko 28, it returns'maybe'. (Firefox bug 884275)

fastSeek(double time)voidDirectly seek to the given time.
load()void Reset the media element and restart selecting the media resource. Any pending events are discarded. How much media data is fetched is still affected by thepreload attribute. This method can be useful for releasing resources after anysrc attribute andsource element descendants have been removed. Otherwise, it is usually unnecessary to use this method, unless required to rescansource element children after dynamic changes.
mozGetMetadata()非标准Object ThemozGetMetadata method returns a javascript object whose properties represent metadata from the playing media resource as {key: value} pairs. A separate copy of the data is returned each time the method is called.
This method must be called after theloadedmetadata event fires.
mozLoadFrom(HTMLMediaElement other)非标准已弃用void This method, available only in old Mozilla's implementation, loads data from another media element. This works similarly toload() except that instead of running the normal resource selection algorithm, the source is simply set to theother element'scurrentSrc.
This is optimized so this element gets access to all of theother element's cached and buffered data; in fact, the two elements share downloaded data so that data downloaded by either element is available to both.
pause()void暂停播放。
play()void开始播放。
setMediaKeys实验性Promise Sets theMediaKeys keys to use when decrypting media during playback.
setSinkId实验性Promise Sets the ID of the audio device through which audio output should be rendered if the application is authorized to play out of a given device.

Events

Audio and Video elements can fire quite a few differentevents.

Specifications

Specification
HTML
# htmlmediaelement

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp