Reputation: 622
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();
});
Upvotes: 0
Views: 1499
Reputation: 3931
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);
});
Upvotes: 2