How can I stop whats currently playing and start playing a new file?
This is how i play sounds, if i call this function again while another sound is playing, i want it to stop and play the new sound.
function playSound(audio) {
var sound = new Howl({
src: ['assets/sound/voice/' + audio]
});
window.setTimeout(function(){
sound.play();
}
}, 400);
}
This is how i do it by create global variable for 'sound' instead local variable.
var sound = null;
function playSound(audio) {
//check if sound is null, if not stop previous sound and unload it
if (sound != null) {
sound.stop();
sound.unload();
sound = null;
}
sound = new Howl({
src: ['assets/sound/voice/' + audio]
});
sound.play();
}
By the way, why did you use setTimeout?
@ikhsanNCC that worked, thanks a lot :)
Most helpful comment
This is how i do it by create global variable for 'sound' instead local variable.
By the way, why did you use setTimeout?