【发布时间】:2014-06-07 12:36:08
【问题描述】:
如果音频元素 src 属性指向不存在的文件,我试图让浏览器显示警报,但是如果我附加“错误”事件,我不会得到任何响应。
这是我的问题以及我尝试过的问题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检测文件没有播放(因为找不到)的错误?
【问题讨论】:
标签: javascript html audio html5-audio