HTML5音频播放器事件操作“已结束”被多次调用

时间:2014-09-10 13:10:41

标签: javascript html5 html5-audio audio-player

我正在使用HTML5音频播放器。我正在实现播放列表。我正在使用'结束'事件动作播放下一个音频媒体文件。但是,在媒体文件的最后,'结束了'事件被解雇了一次以上。 (因为我记录的消息多出一次。)

这是我的事件监听器:

    //Event Listener
function addPlayerListener() {
    console.log("Recording Index: "+combinedSessionIndex);

    var audio = document.getElementById("playarea");
    audio.addEventListener('ended', function () {

        sessionsToPlay = $("#audio_files").val().split(',');

        console.log ("In Recording End Event Listener...!!!"+combinedSessionIndex);
        //It Shows Length '1' not '0'
        if (sessionsToPlay.length == 1) {
            console.log("Returned Due to Session To Play length..!!!")
            return;
        }

        //As CombinedSessionIndex Starts with 1
        if (combinedSessionIndex == sessionsToPlay.length) {
            console.log("Returned Due to All Session To Play Got Finish..!!!")
            $("#audio_files").val("");
            sessionsToPlay = [];
            combinedSessionIndex = 0;
            return;
        }

        var filePath = '${properties["RECORDED_SESSION_RETREIVAL_PATH"]}';
        filePath = filePath + sessionsToPlay[combinedSessionIndex] + ".mp4";
        combinedSessionIndex++;       
        audio.src = filePath;
        audio.play();
    }, false);
}

因此,当特定媒体文件结束时,它会跳过下一个媒体文件并开始播放下一个(跳过1,2)文件后。对于每个跳过的媒体文件,控件都来自事件监听器,它表示在单个呼叫结束时正在重复调用事件监听器。

第一次使用此代码播放音频文件:

  //PlayBack Session
function play(data) {

    //Just to deal with a scenario that when we play a combined session and in the mean while we start listening any other single
    //session, then after completion of this single session, remaining previous combined session should not resume.
    //console.log("Re-setting Player ...!!!");
    //$("#audio_files").val("");
    //sessionsToPlay = [];
    //combinedSessionIndex = 0;

    var filePath = '${properties["RECORDED_SESSION_RETREIVAL_PATH"]}';
    filePath = filePath + data + ".mp4";
    console.log("filePath: " + filePath);
    if (data == null || data == "") {
        alert("Nothing to Play ...!!!");
        return;
    }
    $("#playarea").attr({
        "src": filePath,
        "autoplay": "autoplay"
    })
}

然后,当此结束时,使用“结束”播放更多剪辑。事件监听。

我无法找到原因。如果我做错了什么请指导我?

提前致谢:)

1 个答案:

答案 0 :(得分:0)

啊,我的坏。我正在注册相同的事件监听器'结束'经常(每次用户按下播放按钮,而它应该只是一次)。这就是为什么我会结束'事件不止一次。它实际上生成了'结束''我注册'结束'的事件行动次数事件

你认为它不应该只考虑一次吗?因为我们的目的只是在媒体文件结束时收到通知。虽然我们多次注册,但没有多次通知的逻辑。

相关问题