0

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();
});
pcproff
  • 612
  • 1
  • 8
  • 30
  • keep a reference to the other sounds that were played and call pause – epascarello Mar 04 '14 at 01:34
  • 1
    This question: [Click Here](http://stackoverflow.com/questions/2988050/html5-audio-player-jquery-toggle-click-play-pause) May be helpful? – Blundering Philosopher Mar 04 '14 at 01:39
  • This still causes multiple clicks to play other songs. This audio element needs an isPlaying:Bool I suspect. Because I have over 8 sounds inside of #messageCenter area. – pcproff Mar 04 '14 at 01:47

1 Answers1

2

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);
});
Alexey Prokhorov
  • 3,431
  • 1
  • 22
  • 25