HTMLMediaElement:canPlayType() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

HTMLMediaElementcanPlayType() 方法用于报告当前浏览器播放指定 MIME 类型媒体的可能性。

语法

js
canPlayType(type)

参数

type

一个指定媒体的 MIME 类型字符串,并可选包含一个 codecs 参数,该参数是一个用逗号分隔的支持的编解码器列表。

返回值

一个表示媒体可播放的可能性的字符串。该字符串将是以下值之一:

""(空字符串)

当前设备无法播放该媒体。

probably

该媒体可能可以在此设备上播放。

maybe

没有足够的信息来确定该媒体是否可以播放(直到实际尝试播放时)。

示例

js
let obj = document.createElement("video");
console.log(obj.canPlayType("video/mp4")); // “maybe”

规范

Specification
HTML
# dom-navigator-canplaytype-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
canPlayType()

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

参见