play()

 

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

HTMLMediaElement.play() 可以让媒体文件尝试播放(video/audio)并返回Promise

语法

HTMLMediaElement.play().then(function() { ... }

返回值

 Promise 如果媒体已经被播放则无法继续播放

Parameters

所谓 Promise ,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。

免责

The promise's rejection handler is called with an exception as its input value (as opposed to a traditional exception being thrown). Possible exceptions include:

NotAllowedError
The user agent (browser) or operating system doesn't allow playback of media in the current context or situation. This may happen, for example, if the browser requires the user to explicitly start media playback by clicking a "play" button.
NotSupportedError
The media source (which may be specified as a MediaStream, MediaSource, Blob, or File, for example) doesn't represent a supported media format.

Other exceptions may be reported, depending on browser implementation details, media player implementation, and so forth.

参考文档

Specification Status Comment
WHATWG HTML Living Standard
play()
Living Standard Initial definition; living specification.
HTML5
play()
Recommendation Initial definition.

Note that the WHATWG and W3C versions of the specification differ (as of April 20, 2016) as to whether or not this method returns a Promise or nothing at all, respectively.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 50.0 3.5 (1.9.1) ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 50.0 (Yes) ? ? ? ? 50.0

文档标签和贡献者