0

I've already asked a question very similar to this but I can't get the suggested code to work. the jquery is:

  jQuery("span").click(function (event) {
    var elementID = event.target.id;
    var oggVar = ("audio/"+elementID +".ogg");
    var audioElement = document.createElement("audio");
    audioElement.setAttribute("src", oggVar); 
    audioElement.play(); 
  });

This works OK, the problem is it works even tho another audio file is playing. I want to prevent this, so that an audio file does not run if another audio file is playing. Sorry to have to revisit this.

RoyS
  • 1,439
  • 2
  • 14
  • 21

1 Answers1

1

This has been answered before, but let me take a shot at this. You're going to want to check to see if any audioclips are playing. I don't think there is a isPlaying sort of built in logic, but I know you can use the event listeners.

var audioIsPlaying = false;

jQuery("span").click(function (event) {
    if(audioIsPlaying != true) {
        var elementID = event.target.id;
        var oggVar = ("audio/"+elementID +".ogg");
        var audioElement = document.createElement("audio");
        audioElement.setAttribute("src", oggVar); 
        audioElement.play();
        audioIsPlaying = true;
        audioElement.addEventListener("ended", function() {
            audioIsPlaying = false;    
        });
    } else {
        //still playing
    }
});
James_1x0
  • 931
  • 10
  • 20