gpt4 book ai didi

javascript 在音频中添加 onend 事件

转载 作者:行者123 更新时间:2023-12-03 06:56:14 24 4
gpt4 key购买 nike

我希望这不会被认为是重复的,因为我已经搜索了许多与我的问题相关的问题。不过,我对 javarscript 和 jquery 完全是菜鸟,所以请记住这一点。

我有以下代码,其中一切正常,但在音频文件完成后,图标不会更改为“播放”,并且文件不会重新加载。我尝试添加 onending 事件,但我不确定具体在哪里以及如何执行。这将为我的项目带来很大帮助,我将不胜感激。谢谢!

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>


</head>
<body>

<div class="btn btn-xs" id="btn1"><i class="glyphicon glyphicon-play"></i></div>

<div class="btn btn-xs" id="btn2"><i class="glyphicon glyphicon-play"></i></div>

<div class="btn btn-xs" id="btn3"><i class="glyphicon glyphicon-play"></i></div>


<!-- SOUNDS -->
<audio id="sound1">
<source src="http://www.internetdj.com/mp3/108215.mp3" type="audio/mpeg" />
</audio>

<audio id="sound2">
<source src="http://www.bigsoundbank.com/sounds/ogg/0580.ogg" type="audio/ogg" />
</audio>

<audio id="sound3">
<source src="http://www.bigsoundbank.com/sounds/ogg/0003.ogg" type="audio/ogg" />
</audio>


</body>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link href="css/bootstrap.css" rel="stylesheet" />
<script type="text/javascript">
$('.btn').click(function () {
var $this = $(this);
var id = $this.attr('id').replace(/btn/, '');
var play = '<div class="btn btn-xs"><i class="glyphicon glyphicon-play"></i></div>';
var pause = '<div class="btn btn-xs"><i class="glyphicon glyphicon-pause"></i></div>';
$this.toggleClass('active');
if ($this.hasClass('active')) {
$this.html(pause);
$('audio[id^="sound"]')[id - 1].play();
} else {
$this.html(play);
$('audio[id^="sound"]')[id - 1].pause();
$('audio[id^="sound"]')[id - 1].pause();
}
});
</script>
</html>

最佳答案

JS

 $('.aud').bind("ended",function(){
var btn_id = $(this).attr("id").replace(/sound/,'');
btn_id = "btn" + btn_id;
var play = '<div class="btn btn-xs"><i class="glyphicon glyphicon-play"></i></div>';
var pause = '<div class="btn btn-xs"><i class="glyphicon glyphicon-pause"></i></div>';


$("#"+btn_id).toggleClass('active');

if(!$("#"+btn_id).hasClass('active')){
$("#"+btn_id).html(play);
}


alert("finished");
});

$('.btn').click(function () {
var $this = $(this);
var id = $this.attr('id').replace(/btn/, '');
var play = '<div class="btn btn-xs"><i class="glyphicon glyphicon-play"></i></div>';
var pause = '<div class="btn btn-xs"><i class="glyphicon glyphicon-pause"></i></div>';
$this.toggleClass('active');
if ($this.hasClass('active')) {
$this.html(pause);
$('audio[id^="sound"]')[id - 1].play();
} else {
$this.html(play);
$('audio[id^="sound"]')[id - 1].pause();
$('audio[id^="sound"]')[id - 1].pause();
}
});

$('.aud').bind("ended",function(){
var btn_id = $(this).attr("id").replace(/sound/,'');
btn_id = "btn" + btn_id;
var play = '<div class="btn btn-xs"><i class="glyphicon glyphicon-play"></i></div>';
var pause = '<div class="btn btn-xs"><i class="glyphicon glyphicon-pause"></i></div>';


$("#"+btn_id).toggleClass('active');

if(!$("#"+btn_id).hasClass('active')){
$("#"+btn_id).html(play);
}


alert("finished");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<body>
<div class="btn btn-xs" id="btn1"><i class="glyphicon glyphicon-play"></i></div>

<div class="btn btn-xs" id="btn2"><i class="glyphicon glyphicon-play"></i></div>

<div class="btn btn-xs" id="btn3"><i class="glyphicon glyphicon-play"></i></div>


<!-- SOUNDS -->
<audio id="sound1" class="aud">
<source src="http://www.internetdj.com/mp3/108215.mp3" type="audio/mpeg" />
</audio>

<audio id="sound2" class="aud">
<source src="http://www.bigsoundbank.com/sounds/ogg/0580.ogg" type="audio/ogg" />
</audio>

<audio id="sound3" class="aud">
<source src="http://www.bigsoundbank.com/sounds/ogg/0003.ogg" type="audio/ogg" />
</audio>
</body>

演示:http://jsbin.com/kirifu/edit?html,js,output

关于javascript 在音频中添加 onend 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262420/

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