我试图做一个浏览器中显示一个警告,如果音频元素src属性指向一个不存在的文件,但我没有得到任何回应,如果我附上了“错误”事件。检测HTML5音频元素文件未找到错误
这里是我的问题,小提琴,并与我曾尝试http://jsfiddle.net/Xx2PW/7/
的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>
而且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!");
});
我怎样才能检测到的错误文件没有被播放(因为没有被发现)与JS?
辉煌,这就是它 - 感谢您的链接到文件为好,一旦你知道它的源元素,这让很多的意义。 – WojtekD
任何想法如何在React组件中做到这一点? – asubanovsky
这对我很好。但是,有没有办法确定导致错误的调用返回的http状态代码,或者错误是什么? – Bauer