I am working with an audio tag whose source can be one of several types but called from an ashx file, as I am dynamically loading the stream.
<audio id="audio" controls="" autoplay="" download="soundFile.wav">
<source id="mp3" src="../Controls/SoundPlayer/GetSoundFile.ashx?soundType=Sound&fileId=test&fileType=mp3" type="audio/mpeg3">
<source id="wav" src="../Controls/SoundPlayer/GetSoundFile.ashx?soundType=Sound&fileId=test&fileType=wav" type="audio/wav">
</audio>
This part works fine, but what I'd like to do is catch an error when it fails. Which I can do like so:
$('audio').on('error', function (e) {
$('#divAudioPlayerMessage').text('Recording can’t be played');
});
$('source').on('error', function (e) {
$('#divAudioPlayerMessage').text('Recording can’t be played');
});
Or like so (which apparently really only works in IE [I got this from How to check if HTML5 audio has reached different errors)
document.getElementsByTagName('audio')[0].addEventListener('error', function(e, o) {
// audio playback failed - show a message saying why
// to get the source of the audio element use $(this).src
var errorMessage = '';
switch (e.target.error.code) {
case e.target.error.MEDIA_ERR_ABORTED:
errorMessage = 'You aborted the video playback.';
break;
case e.target.error.MEDIA_ERR_NETWORK:
errorMessage = 'A network error caused the audio download to fail.';
break;
case e.target.error.MEDIA_ERR_DECODE:
errorMessage = '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:
errorMessage = 'The video audio not be loaded, either because the server or network failed or because the format is not supported.';
break;
default:
errorMessage = 'An unknown error occurred.';
break;
}
$('#divAudioPlayerMessage').text(errorMessage);
}, true);
My question is how do I tell what the actual error is. For example, in my ashx file I might return a 404 error, which if I get I would like to give a specific message.
As well, my other problem, is that when running in Chrome or Firefox, who do not support mp3, my error handler will be called, but I would like to ignore that error since I also have a wav file which is supported.
Thank you!