具体来说,如果 206 个音频请求中的一个失败并且缓冲停止,有没有办法检测到该状态?或者我是否应该通过将缓冲量与过去的量进行比较来检查缓冲是否停止?
另外,我如何检查指定的来源是否失败,然后您可以将其指向另一个来源吗?
具体来说,如果 206 个音频请求中的一个失败并且缓冲停止,有没有办法检测到该状态?或者我是否应该通过将缓冲量与过去的量进行比较来检查缓冲是否停止?
另外,我如何检查指定的来源是否失败,然后您可以将其指向另一个来源吗?
1. 具体来说,如果其中一个音频请求失败并且缓冲停止,有没有办法检测到该状态?
是的,有几种方法可以做到这一点!但是,如果您想捕获错误类型,可以将错误事件侦听器附加到源:
$('audio').addEventListener('error', function failed(e) {
// audio playback failed - show a message saying why
// to get the source of the audio element use $(this).src
switch (e.target.error.code) {
case e.target.error.MEDIA_ERR_ABORTED:
alert('You aborted the video playback.');
break;
case e.target.error.MEDIA_ERR_NETWORK:
alert('A network error caused the audio download to fail.');
break;
case e.target.error.MEDIA_ERR_DECODE:
alert('The audio playback was aborted due to a corruption problem or because the video used features your browser did not support.');
break;
case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
alert('The video audio not be loaded, either because the server or network failed or because the format is not supported.');
break;
default:
alert('An unknown error occurred.');
break;
}
}, true);
2. 你能把它指向另一个来源吗?
在错误处理函数中,您可以使用src
音频元素的属性更改源:
var audio = $(this);
audio.src = "new-audio-file.mp3";
audio.load();
另一种选择是使用以下语法将多个源添加到同一个音频标签:
<audio>
<source id="audio_player_ogv" src="test.ogv" type="audio/ogg" />
//In case that you can't load the ogv file it will try to load test.mp3
<source id="audio_player_mp3" src="test.mp3" type="audio/mpeg" />
</audio>
3.关于管理多个音频文件
如果您正在考虑管理 206 个音频文件,我建议您使用插件。我一直在使用SoundManager2,它非常好!
规范中有媒体元素处理的事件的完整列表:https ://html.spec.whatwg.org/multipage/embedded-content.html#media-elements
我会特别关注停滞、中止、进展事件。
由于此元素相对较新,因此实现可能会有很大差异。因此,我将针对您所针对的平台测试这些事件,以查看它们是否按预期工作以满足您的需求。如果不是,您可能需要做一些更手动的事情,例如轮询您提到的缓冲状态。
我想你可以在页面末尾的w3schools找到你的答案(媒体事件块)
可能onerror
或onstalled
或onreadystatechange