如何检查是否HTML5音频已达到不同的错误 [英] How to check if HTML5 audio has reached different errors

查看:1590
本文介绍了如何检查是否HTML5音频已达到不同的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是,如果音频206请求之一失败和缓冲停止,有没有方法来检测这种状态?或者我应该检查是否缓冲顺道过去对数量比较缓冲的数额?

Specifically, if one of the 206 requests for audio fails and buffering stops, is there a way to detect that state? Or should I have to check if buffering stopped by comparing the buffered amounts against past amounts?

此外,我怎么能检查是否指定的源失败,你能然后指向另外一个来源?

Also how can I check if the specified source fails, could you then point it to another source?

推荐答案

1。特别是,如果音频请求的一个失败,缓冲停止,有没有方法来检测这种状态?

是的,有一些方法来做到这一点!但是,如果你想赶上错误类型可以将错误事件侦听器附加到源:

Yes, there is few ways to do this! But if you want to catch the error type you can attach the error event listener to the sources:

$('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 属性可以更改源:

Inside the error handler function you can change the source using the src property of the audio element:

var audio = $(this);
audio.src = "new-audio-file.mp3";
audio.load();

另一种方法是使用下面的语法多个源添加到相同的音频标签:

Another option is to add multiple source to the same audio tag using this syntax:

<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 一会儿被,这是非常好的!

I would suggest to use a plugin if you are thinking to manage 206 audio files. I have been using SoundManager2 for a while and it's very good!

这篇关于如何检查是否HTML5音频已达到不同的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆