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

查看:18
本文介绍了如何检查 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天全站免登陆