gpt4 book ai didi

javascript - JQuery 获取/设置当前时间 "Radio Stations"

转载 作者:行者123 更新时间:2023-11-28 03:48:26 26 4
gpt4 key购买 nike

我想要实现的效果是,将鼠标悬停在某些图标上将播放某些音频文件。当您切换图标时,新音频将从最后一个音频停止的确切时间开始播放 - 这应该就像您在 radio 上更改 channel 一样。

下面的代码有效,但仅在两个音频文件之间有效,并且它们绑定(bind)到同一个按钮。

如何复制这种效果,但要使用 10 个“站”,每个站都有自己的图标?

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script>
var events=["abort","canplay","canplaythrough","durationchange","emptied","ended",
"error","loadeddata","loadedmetadata","loadstart","pause","play","playing",
"progress","ratechange","readystatechange","seeked","seeking","stalled",
"suspend","timeupdate","volumechange","waiting"]
$(function(){
var current={},
audio=$("audio")
audio.on("play",function(){
current = this // remember
})
$(".icon").hover(function(){
var last=$(this).text()
switch ( last ) {
case "start":
audio.first().trigger("play")
$(this).text("switch")
break;
case "switch":
var next=audio.not(current).prop("currentTime", current.currentTime).trigger("play")
audio.not(next).trigger("pause")
break
}

})
})
</script>
</head>
<body>
<div class="icon">start</div>

<audio class="radio1" controls>
<source src="assets/radio1.mp3" type="audio/mpeg">
</audio>

<audio class="radio2" controls>
<source src="assets/radio2.mp3" type="audio/mpeg">
</audio>
</body>
</html>

https://jsfiddle.net/g00dmhfh/

最佳答案

无需添加多个图标。您可以通过创建变量 currentnext

来控制播放哪个
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>perfect</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script>
var events=["abort","canplay","canplaythrough","durationchange","emptied","ended",
"error","loadeddata","loadedmetadata","loadstart","pause","play","playing",
"progress","ratechange","readystatechange","seeked","seeking","stalled",
"suspend","timeupdate","volumechange","waiting"]
$(function(){
var current = -1;
var next = 0;
var aAudio = $('audio');
var audioLen = aAudio.length;
$(".icon").click(function(){
if(audioLen <= 0){
return false;
}else if(audioLen == 1){
aAudio.eq(next).trigger('play');
}else{
if(current == -1){
$(this).text("switch");
}else{
aAudio.eq(current).trigger('pause');
}
aAudio.eq(next).trigger('play');
current = next;
next = next == audioLen - 1?0:next+1;
}
console.log(current +'~~~~~'+ next);

})
})
</script>
</head>
<body>
<div class="icon">start</div>
<div class="m-audio-wrap">
<audio class="radio1" controls>
<source src="assets/radio1.mp3" type="audio/mpeg">
</audio>

<audio class="radio2" controls>
<source src="assets/radio2.mp3" type="audio/mpeg">
</audio>
<audio class="radio1" controls>
<source src="assets/radio1.mp3" type="audio/mpeg">
</audio>

<audio class="radio2" controls>
<source src="assets/radio2.mp3" type="audio/mpeg">
</audio>
</div>
</body>
</html>

关于javascript - JQuery 获取/设置当前时间 "Radio Stations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48245822/

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