gpt4 book ai didi

javascript - html 音频 - 手动停止音频,引发结束事件

转载 作者:行者123 更新时间:2023-11-28 07:00:55 25 4
gpt4 key购买 nike

我可以手动停止 html 音频以引发“结束”事件吗?

<script>
var audio = new Audio();
audio.src = "https://translate.google.com/translate_tts?&q=text&tl=en&client=a"
audio.play();

audio.addEventListener("ended", function() {
console.log("audio ended");
});

audio.addEventListener("play", function() {
window.setTimeout(function() {
audio.pause(); //stop audio half sec after it starts - this doesn't raise the "ended" event!
}, 500);
});
</script>

最佳答案

你可以做到这一点,但它是在 timeupdate 事件下面分别使用 jQuery 和 JS 的代码

<audio ontimeupdate="watchTime4Audio(this.currentTime,this.duration)" ... ></audio>

在 HTML 中

//jQuery
$('audio').on('timeupdate',function(){
if($(this).prop('currentTime')>=$(this).prop('duration')){
//the song has ended you can put your trigger here
}
});

//JS
audio.addEventListener('timeupdate', function() {
if(this.currentTime >= this.duration){
//the song has ended you can put your trigger here
}
});

使用 Javascript/JQuery

关于javascript - html 音频 - 手动停止音频,引发结束事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com