音频播放完成后触发事件

时间:2015-12-13 02:50:38

标签: javascript jquery html5 audio

我在HTML5中的<audio>标记上没有看到太多文档。我正试图在音频播放完毕后触发事件。

HTML:

<audio id="audio">
    <source src="'.$query['audioPronunciation'].'" type="audio/mp3">
    Your browser does not support the audio element.
</audio>

JavaScript / jQuery Play&amp;暂停:

function audio() {
    if ($("#audioControl").hasClass("glyphicon-play")) {
        $("#audioControl").addClass("glyphicon-pause").removeClass("glyphicon-play");
        $("#audio").trigger("play");
    } else if ($("#audioControl").hasClass("glyphicon-pause")) {
        $("#audioControl").removeClass("glyphicon-pause").addClass("glyphicon-play");
        $("#audio").trigger("pause");
    }
}

JavaScript / jQuery,'结束时'

$("#audio").addEventListener('ended', function() {
    alert("Audio has finished playing!");

});

1 个答案:

答案 0 :(得分:2)

您不需要事件监听器,只需:

var audio1 = document.getElementById("myAudio1");
audio1.onended = function() {
    alert("audio playback has ended");
};