Javascript alert block blocking sound object

I have a little problem. Basically, what I'm trying to do is make the application appear in the box when playing a certain sound. Here is part of the error code:

    var audio = new Audio('song.mp3');
    audio.play();
    alert(1);

      

The problem is that the sound plays after the warning window. I am assuming this is happening because the application does not download the song file right away, but I have an idea how can I do this?

+3


source to share


1 answer


The canplay event occurs when the browser can start playing the specified audio / video (if it is buffered enough to start).

So try this:



var audio = new Audio('song.mp3');
    audio.oncanplay  = function() {
        audio.play();
        alert("1");
    };

      

+6


source







All Articles