html5判定音频状态,javascript – 如何检查HTML5音频是否达到不同的错误

具体来说,如果其中一个音频请求失败和缓冲停止,是否有一种方法来检测该状态?

是的,几乎没有办法做到这一点!但是,如果要捕获错误类型,可以将错误事件侦听器附加到源:

$('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);

你能指出另一个来源吗?

在错误处理函数内部,您可以使用音频元素的src属性更改源:

var audio = $(this);

audio.src = "new-audio-file.mp3";

audio.load();

另一种选择是使用以下语法将多个源添加到同一个音频标签中:

//In case that you can't load the ogv file it will try to load test.mp3

关于管理多个音频文件

如果您正在考虑管理206个音频文件,我建议使用一个插件.我一直在使用SoundManager2一段时间,这是非常好的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值