HTMLMediaElement - Web API 接口参考 编辑

HTML媒体元素接口在属性和方法中添加了 HTML元素来支持基础的媒体相关的能力,就像audio和video一样。HTML 视频元素和 HTML 音频元素元素都继承自此接口。
  <div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 30%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 180" preserveAspectRatio="xMinYMin meet"><a xlink:href="https://developer.mozilla.org/wiki/zh-CN/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25  121,20  121,30  111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/zh-CN/docs/Web/API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25  236,20  236,30  226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/zh-CN/docs/Web/API/Element" target="_top"><rect x="266" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Element</text></a><polyline points="341,25  351,20  351,30  341,25" stroke="#D4DDE4" fill="none"/><line x1="351" y1="25" x2="381" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/zh-CN/docs/Web/API/HTMLElement" target="_top"><rect x="381" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="436" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLElement</text></a><polyline points="491,25  501,20  501,30  491,25" stroke="#D4DDE4" fill="none"/><line x1="501" y1="25" x2="509" y2="25" stroke="#D4DDE4"/><line x1="509" y1="25" x2="509" y2="90" stroke="#D4DDE4"/><line x1="509" y1="90" x2="492" y2="90" stroke="#D4DDE4"/><a xlink:href="/wiki/zh-CN/docs/Web/API/HTMLMediaElement" target="_top"><rect x="331" y="65" width="160" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text  x="411" y="94" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLMediaElement</text></a></svg></div>
  a:hover text { fill: #0095DD; pointer-events: all;}

特性

从父级 HTMLElement, Element, Node, 和 EventTarget 继承属性

名称类型描述
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反映在HTML mediagroup 标签上。 表示元素所归属的分组,同一组的媒体元素会共享同一个控制器(controller)。
mediaKeys 只读MediaKeysReturns a reference to the MediaKeys interface, which is a set of keys that an associated HTMLMediaElement can use for decription of media data during playback.
mozAudioChannelType DOMStringCan 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. See Using the AudioChannels API for more details.
mozChannels只读 long声道数 (比如 2 是立体声).
mozFrameBufferLength long

Indicates the number of samples that will be returned in the framebuffer of each MozAudioAvailable 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).

The mozFrameBufferLength property can be set to a new value, for lower latency, or larger amounts of data, etc. The size given must 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 the loadedmetadata event fires, when the audio info is known, but before the audio has started or MozAudioAvailable events have begun firing.

mozSampleRate只读 long播放内容的采样率(每秒采样次数)。比如,44100 就是一张CD的采样率。
mozSrcObjectMediaStreamLets you set or get the Media Stream to be played or being played.
mutedBoolean静音时为true ,否则是false .
networkStateunsigned short获取媒体时的网络状态
常量定义描述
NETWORK_EMPTY0还没数据。readyState 是 HAVE_NOTHING.
NETWORK_IDLE1
NETWORK_LOADING2正在加载.
NETWORK_NO_SOURCE[1]3
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 the playbackRate 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 range 0.25 and 5.0).

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

played 只读TimeRanges媒体可被播放的范围。
preloadDOMStringReflects the preload HTML attribute, indicating what data should be preloaded, if any. Possible values are: none, metadata, auto. See preload attribute documentation for details.
readyState只读unsigned shortThe readiness state of the media.
ConstantValueDescription
HAVE_NOTHING0No information is available about the media resource.
HAVE_METADATA1Enough of the media resource has been retrieved that the metadata attributes are initialized.  Seeking will no longer raise an exception.
HAVE_CURRENT_DATA2Data is available for the current playback position, but not enough to actually play more than one frame.
HAVE_FUTURE_DATA3Data 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_DATA4Enough data is available—and the download rate is high enough—that the media can be played through to the end without interruption.
seekable只读TimeRangesThe time ranges that the user is able to seek to, if any.
seeking只读BooleanIndicates whether the media is in the process of seeking to a new position.
sinkId 只读 DOMStringThe 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 the MediaDeviceInfo.deviceid values returned from MediaDevices.enumeratedDevices(), id-multimedia, or id-communications.
srcDOMStringReflects the src HTML attribute, containing the URL of a media resource to use. Gecko implements a similar functionality for streams: mozSrcObject.
textTracksTextTrackListRepresents the list of TextTrack objects contained in the element.
videoTracksVideoTrackListRepresents the list of VideoTrack 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(inDOMStringtype)

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.
  • The empty string: if the specified type definitely cannot be played.
Determines whether the specified media type can be played back.

Note: Previously canPlayType('video/webm') returned 'probably'. Starting with Gecko 28 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25 / Firefox OS 1.3), it returns 'maybe'. (bug 884275)

fastSeek(double time)voidDirectly seek to the given time.
load()voidReset the media element and restart selecting the media resource.  Any pending events are discarded.  How much media data is fetched is still affected by the preload attribute.  This method can be useful for releasing resources after any src attribute and source element descendants have been removed.  Otherwise, it is usually unnecessary to use this method, unless required to rescan source element children after dynamic changes.
mozGetMetadata()ObjectThe mozGetMetadata 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 the loadedmetadata event fires.
mozLoadFrom(HTMLMediaElement other) voidThis method, available only in old Mozilla's implementation, loads data from another media element. This works similarly to load() except that instead of running the normal resource selection algorithm, the source is simply set to the other element's currentSrc.
This is optimized so this element gets access to all of the other 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 PromiseSets the MediaKeys keys to use when decrypting media during playback.
setSinkId PromiseSets 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 different events.

Specifications

SpecificationStatusComment
HTML Living Standard
HTMLMediaElement
Living StandardNo change from HTML5
HTML5
HTMLMediaElement
RecommendationInitial definition.
Unknown
Encrypted Media Extensions
UnknownAdds MediaKeys,  MediaEncryptedEvent, and setMediaKeys.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:64 次

字数:26880

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文