I am trying to make a the browser display an alert if an audio element src attribute points to a non existent file, however I do not get any response if I attach the the \"
You actually need to bind to the source tag for listening to error event when willing to detect "file not found error". Have a look at this fiddle.
HTML:
Non existent audio files - click to play
Script:
$("#player1").on("click", function () {
//I've tried catching the error like this - no effect
alert("Trying to play file.");
try {
$('audio')[0].play();
} catch (e) {
alert("Error playing file!");
}
});
$("audio").on("error", function (e) {
alert("Error at audio tag level!");
});
// try this then
$("#wav").on("error", function (e) {
alert("Error with wav file!");
});
$("#mp3").on("error", function (e) {
alert("Error with mp3 file!");
});
$("#ogg").on("error", function (e) {
alert("Error with ogg file!");
});
It is described in this MDN article - section error handling. Let me know if it works for you.