My code is below. Basically, my goal is once the user clicks certain button, I would get this button's id and send it through ajax, then return the mp3 url based on the id specified and play the sound. Everything is working perfectly fine, but the problem is I do not know how to stop or pause the music. I've tried audio.stop()
, or audio.pause()
. Neither one is working.
Once the user clicks certain button, i would like to stop all the music and play the new one that is clicked.
$('.play_sound').click(function(event){
event.preventDefault();
var data_id = $(this).parents('tr').attr('data-id');
$.post('ajax/play_sound.php', {data_id: data_id}, function(data){
var audio = new Audio(data);
audio.play();
});
});