canPlayType背后的原理是什么?

What is the rationale behind canPlayType?

本文关键字:是什么 背后 canPlayType      更新时间:2023-09-26

所以,我最近在Twitter上看到了一个关于canPlayType的参考。基本上总结是:

The canPlayType() method checks if the browser can play the specified audio/video type.
The canPlayType() method can return one of the following values:
"probably" - the browser most likely supports this audio/video type
"maybe" - the browser might support this audio/video type
"" - (empty string) the browser does not support this audio/video type

为什么它的行为不像一个典型的API,只是返回真/假?为什么要用模棱两可的词"也许"answers"可能"呢?

为什么?因为这是规范要求的http://dev.w3.org/html5/spec-preview/media-elements.html#dom-navigator-canplaytype

如果类型是用户代理知道它不能呈现的类型或类型为"应用程序/八字节流",则canPlayType(type)方法必须返回空字符串;如果用户代理确信该类型代表一个媒体资源,它必须返回"probably",如果与这个音频或视频元素一起使用,它可以呈现;否则它必须返回"maybe"。鼓励实现者返回"maybe",除非可以确定该类型是否被支持。通常,如果codecs参数不存在,用户代理不应该为允许使用该参数的类型返回"probably"。