当用户在播放音频时关闭窗口时显示警报

Show a alert when the user close the window when the audio is playing

本文关键字:窗口 显示 播放 音频 用户      更新时间:2023-09-26

我使用MediaElement.js框架来构建我的音频播放器...但是,是一个播客,正常看到用户意外关闭窗口。

我正在寻找脚本,但绝对没有任何效果。

当用户关闭窗口时播放器工作时,如何提醒用户...?

如果正在播放:询问用户是否真的想要这个。如果玩家没有玩:关闭窗口。

添加事件,如下所示:

$(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?";
    }
})

如果您的元素在页面上并且未存储在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...
}