2017-09-26 159 views
0

如何处理音频视频html5中不存在的音源?如何处理音频视频html5中不存在的音源?

我想https://www.w3schools.com/TAgs/av_event_error.asp: 纯JavaScript

<audio controls onerror="alert(1);"> 
    <source src="http://somethingthatnotexist" type="audio/mpeg"> 
</audio> 

与jQuery

<audio controls "> 
    <source src="http://somethingthatnotexist" type="audio/mpeg"> 
</audio> 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
<script> 
    $(function(){ 
     $('audio').on('error', function(){ 
      alert(1); 
     }) 
    }); 
</script> 

,但没有解决呢。

正确的方法做,这是唯一的地方财产src<video>标签内部没有<source>标签。像这样: 作品在JavaScript和jQuery的

<audio id="myaudio" controls src="http://somethingthatnotexist"></audio> 

但是这看起来akward的对我来说,这是因为:

  1. 我需要我的客户

  2. 这么多提供超过1个音频播放器插件/库使用<audio><source src=""></source></audio>而不是<audio src=""></audio>

感谢您的帮助。

回答

1

试一下这个

<audio controls> 
    <source id="my_audio" src="http://somethingthatnotexist" type="audio/mpeg"> 
</audio> 

$("#my_audio").on("error", function (e) { 
    alert("Error with source file!"); 
}); 
+0

不错。非常感谢你。我会更新我的问题,这样你的答案会看起来清晰,适合像我这样的同样问题的每个人 – plonknimbuzz

0

更简单和常见的方式是这样的:

<audio controls> 
    <source src="http://somethingthatnotexist" type="audio/mpeg"> 
    Error with source file! 
</audio> 

如果音频文件不能因为某些原因被播放时,包含的文字将会出现。 (不是在警报中,而是在玩家GUI中)