2017-09-26 139 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中)