我使用jQuery创建了一个音频对象,就像这样
var player;
$(document).ready(function() {
player = new Audio();
player.id = "player";
player.volume = 0.5;
});
我能够玩,暂停,......成功
player.src = file;
player.play();
这是我遇到麻烦的时候。我试图检测播放器何时停止播放,但它无法正常工作......
我尝试过以下方法但没有成功:
$(document).on("ended", "#player", function(e) {
alert('stop');
});
$("#player").bind("ended", function() {
alert('stop');
});
$("#player").on("ended", function(e) {
alert('stop');
});
如何判断玩家何时停止使用jQUERY?
答案 0 :(得分:2)
你离我很近但应该addEventListener
使用ended
event
$(document).ready(function() {
player = new Audio();
player.volume = 0.5;
var playerID = document.getElementById('player');
playerID.addEventListener("ended", function(e){
alert('ended');
}, false);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<audio controls id="player">
<source src="https://www.w3schools.com/html/horse.ogg" type="audio/ogg">
<source src="https://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
&#13;
w3schools的音频信用
答案 1 :(得分:2)
为什么不将html5
音频元素与纯js
一起使用?
<强> W3Schools 强>
var aud = document.getElementById("myAudio");
aud.onended = function() {
alert("The audio has ended");
};