如何检查浏览器是否支持新音频?

tot*_*ssi 5 javascript audioformat html5-audio

在 safari 5 中,不支持新音频,因此错误控制台显示:

TypeError : 'undefined' is not a constructor (evaluating 'new Audio')
Run Code Online (Sandbox Code Playgroud)

我如何以编程方式知道浏览器是否支持新音频?

Ter*_*nen 3

我想你可能会尝试一下...

var createAudio = function() {
 try {
    return new Audio(); 
 } catch(e) {
    return false;
 }
};
var audio = createAudio();
if(audio) {
  // start playing... or check formats etc.
}
Run Code Online (Sandbox Code Playgroud)

如果出现异常,则 Audio 类不存在并返回 false。

有关更详细的解决方案,请检查 Modernzr 库:http://modernizr.com/docs/#audio

相关的解决方案在这里Detecting html5 audio support with Modernizr