【发布时间】:2012-10-05 04:34:21
【问题描述】:
我正在使用<audio> 标签在多个浏览器中播放音频文件。
var audioTag = document.createElement("audio"),
sourceTag = document.createElement("source"),
sorryTag = document.createElement("div");
sorryTag.innerHTML = "This filetype not supported";
audioTag.onerror = function() {
//some error handling code
}
sourceTag.onerror = function() {
/some error handling code
}
sourceTag.src = "myfile.mp3";
audioTag.appendChild(sourceTag);
audioTag.appendChild(sorryTag);
//add audioTag to DOM
这导致
<audio>
<source src='myfile.mp3' />
<div>This filetype not supported</div>
</audio>
Firefox 不能播放 MP3 文件,我可以接受。 Mozilla also promises 表示,如果 <audio> 或 <video> 标签无法播放媒体,将调度 error 事件。它还将逐个检查嵌套在媒体标签中的标签(<source> 或其他标签,最后一个可能是错误消息),直到找到可以使用的标签。这些似乎都不适合我;元素上永远不会触发错误事件,也不会显示错误消息。我做错了什么?
【问题讨论】:
-
如果你使用
-
不可能。我在设置
source标记的src属性之前添加了onerror事件侦听器。更新代码以反映这一点。 -
对于
video标签,该事件在其他浏览器上触发得很好,只是在audio标签的FF上没有 -
嗯。这很奇怪。是否可以发布指向显示问题的页面的链接?
-
很遗憾没有。这是一项工作,目前正在开发中。我找到了一种解决方法(基于在超时后检查音频元素的 networkState 属性),一旦我验证它工作正常,我将在下面发布。
标签: firefox cross-browser html5-audio