Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect error type for Audio tag with Sources

It appears that before the <source> element was introduced for <audio> tags, when the audio had an error you could see the error code in audio.error.code. However, this doesn't seem to happen anymore. Since the error events are now only fired on the child <source> elements and no longer on the audio tag, the audio tag no longer has an error property (it's always null). The source tags don't get an error property either.

You can see this in this jsFiddle.

How are you suppose to detect the error type now that the audio tag doesn't get an error property? It seems that this is bug in every browser.

like image 921
Steven Lambert Avatar asked Sep 19 '14 19:09

Steven Lambert


People also ask

What does this code do audio controls source?

The HTML <audio> controls attribute is used to specify the control to play audio. It is the Boolean value. This attribute is new in HTML5.

Can we embed an audio file with source tag?

The <source> element allows you to specify alternative audio files which the browser may choose from. The browser will use the first recognized format. The text between the <audio> and </audio> tags will only be displayed in browsers that do not support the <audio> element.

Which type of files are used along with audio tag?

The <audio> tag references one or more audio files with a src attribute or the <source> element. The browser will choose the first file with a file format that it supports. Supported audio file formats include MP3, WAV, and OGG.


2 Answers

onerror will fire if you add true after the function.

var audio = document.getElementById('audio');    
audio.addEventListener('error', function(e) {
    var noSourcesLoaded = (this.networkState===HTMLMediaElement.NETWORK_NO_SOURCE);
    if(noSourcesLoaded) console.log("could not load audio source");
}, true);
like image 68
epascarello Avatar answered Sep 19 '22 07:09

epascarello


In Angular you can fire this event to know if aodio did load or not.

 this.audio.addEventListener('error', ()=> {
 console.log("could not load audio source");
 ///Do your thing 
 });
like image 31
Vishesh Katoch Avatar answered Sep 20 '22 07:09

Vishesh Katoch