Pergunta

I use the MediaElement.js framework to build my audio players... But, is a podcast and is normal see users closing the window accidentally.

I'm searching for scripts to this, but absolutely nothing works.

How alert the user when he close the window with the player working...?

If is playing: ask if the user really want this. If the player isn't playing: close the window..

Foi útil?

Solução

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...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top