Sto tentando di far visualizzare al browser un avviso se un attributo src di un elemento audio punta a un file inesistente, tuttavia non ottengo alcuna risposta se allego l'errore " evento.Rilevamento errore file audio HTML5 non trovato errore
Ecco un violino con il mio problema e con quello che ho provato http://jsfiddle.net/Xx2PW/7/
Il codice HTML:
<p>Non existent audio files - should return an error
<audio preload="auto">
<source src="http://example.com/non-existant.wav" />
<source src="http://example.com/non-existant.mp3" />
<source src="http://example.com/non-existant.ogg" />
</audio>
</p>
<p>Existing audio file - should just play
<audio preload="auto">
<source src="http://jplayer.org/audio/m4a/Miaow-07-Bubble.m4a" />
</audio>
</p>
E il JS:
playerMarkup = "<a class=\"player\">Play</a>";
audioTags = $("audio");
audioTags.before(playerMarkup); //insert markup before each audio tag
$(".player").on("click", function() {
currentAudio = $(this).next()[0];
//I've tried catching the error like this - no effect
alert("Trying to play file.");
try {
currentAudio.play();
} catch (e) {
alert("Error playing file!");
}
});
//I've also tried just handling the event error - no effect
audioTags.on("error", function (e) {
alert("Error playing file!");
console.log("Error playing file!");
});
Come posso rilevare un errore del file non viene riprodotto (a causa di non essere trovato) con JS?
Brillante, è così - grazie anche per il link alla documentazione, una volta che sai che è l'elemento sorgente, ha molto senso. – WojtekD
Qualche idea su questo nel componente React? – asubanovsky
Questo ha funzionato alla grande per me. Tuttavia, esiste un modo per determinare il codice di stato http restituito dalla chiamata che ha causato l'errore o l'errore? – Bauer