Question

I am playing audio files just fine but I need to stop any other sounds once I click on any other file within #messageCenter area. There is no isPlaying parameter inside of is there an easy to do this? The only other reference to this in Stack was a gaming plugin.

$("#messageCenter area").on( "click", function() {
        var source = "assets/audio/" + $(this).attr('id') + ".wav";
        var audio = $('#audio');
        var sound = new Audio(source);
        sound.play();
});
Was it helpful?

Solution

Try the following code (sorry, I didn't test it):

$("#messageCenter area").on( "click", function() {
    var audio = $('#audio'); 
    var lastSound = audio.data('lastSound');
    if (lastSound) lastSound.pause();

    var source = "assets/audio/" + $(this).attr('id') + ".wav";
    var sound = new Audio(source);
    sound.play();

    audio.data('lastSound', sound);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top