简体   繁体   中英

how to check if audio file link is not loaded in html5 tag

what is the fastest way to check if audio link is invalid (and alert)?

<audio controls src="http://example.com/file/notfound.mp3">
 browser not supported
</audio>

JSFIDDLE

Have you tried this?

var myAudio = document.getElementById('myAudio');
myAudio.onerror = function(error) {

  console.error(error);
}

With HTML:

<audio id="myAudio" controls src="http://example.com/file/notfound.mp3">
  browser not supported
</audio>

Here's the fiddle .

After researching little bit, this is what I found:

$("#postaudiotag").on("click", function () {
    try {
        $('audio')[0].play();
    } catch (e) {
            alert('error playing file')
    }
});

$("#audiomp3").on("error", function (e) {
    alert('mp3 file is not found');
});

demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM