Pergunta

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();
});
Foi útil?

Solução

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);
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top