Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if HTML5 audio has reached different errors

Tags:

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?

like image 819
Neablis Avatar asked Nov 28 '12 21:11

Neablis


People also ask

What is the correct HTML5 element for playing audio files?

The HTML <audio> element is used to play an audio file on a web page.

Can I play audio in HTML True or false?

There are three supported audio formats in HTML: MP3, WAV, and OGG.

Which audio format is not supported by HTML5?

aLaw audio format is not supported by HTML5 audio tag.

How is playing audio and video in HTML5.0 and give example?

HTML5 features include native audio and video support without the need for Flash. The HTML5 <audio> and <video> tags make it simple to add media to a website. You need to set src attribute to identify the media source and include a controls attribute so the user can play and pause the media.


2 Answers

1. Specifically, if one of the requests for audio fails and buffering stops, is there a way to detect that state?

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. Could you then point it to another source?

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. About manage multiple audio files

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!

like image 95
Tom Sarduy Avatar answered Sep 23 '22 10:09

Tom Sarduy


There is a complete list of the events handled by media elements in the spec: https://html.spec.whatwg.org/multipage/embedded-content.html#media-elements

I would look specifically at the stalled, abort, progress events.

Since this element is relatively new implementations may vary greatly. So I would test these events against the platforms you are targeting to see if they work as expected for your needs. If not you may need to do something a bit more manual like polling the buffered state as you had mentioned.

like image 40
cjgammon Avatar answered Sep 21 '22 10:09

cjgammon