HTMLMediaElement.canPlayType()

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

 HTMLMediaElement.canPlayType() 方法会判断传递的媒体格式参数是否能够被播放。

Note: This feature is not available in Web Workers.

语法

str = audioOrVideo.canPlayType(mediaType);

参数

mediaType
DOMString包含了媒体文件的MIME类型。

返回值

String. 有可能的值为:

  • 'probably': 这种媒体文件似乎是可播放的。
  • 'maybe': 不能告诉你这种媒体文件是否能被播放,直到你尝试播放它。
  • '' (empty string): 这种媒体文件不能被播放。

提醒: 以前 canPlayType('video/webm') 会返回 'probably'。从 Gecko 28 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25 / Firefox OS 1.3) 开始, 将返回 'maybe'。 (bug 884275)

示例

var obj = document.createElement('video');
console.log(obj.canPlayType('video/mp4')); // "maybe"

规范

Specification Status Comment
HTML Living Standard
HTMLMediaElement.canplaytype
Living Standard HTML5后没有变更
HTML5
HTMLMediaElement.canplaytype
Recommendation 初始定义.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
canPlayType()Chrome Full support YesEdge Full support 12Firefox Full support 3.5
Notes
Full support 3.5
Notes
Notes Prior to Firefox 28, canPlayType() returned probably when asked about WebM audio or video files without the codecs parameter. Since multiple codecs are supported, this is not enough information to determine if a file can be played, so maybe is now correctly returned.
IE Full support 9Opera Full support YesSafari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

其他