Add an event as follows:
$(window).bind("beforeunload", function(evt) {//gets called before the user leaves the page
//check if audio is already running
if(isPlayingAudio()) {
//shows a confirm message asking user if they want to stay
return "Are you sure you want to leave while playing music?";
}
})
If your elements are on the page and not stored in javascript
function isPlayingAudio() {
var $playing = $('audio').filter(function(i, $audio) {//all audio not paused
return !$audio.prop('paused');
});
return $playing.length === 0;//no audio elements are playing...
}