我试图实现的效果是,将鼠标悬停在某些图标上将播放某些音频文件。当您切换图标时,新音频将从上一个音频停止播放的确切时间开始播放-就像您要更改广播中的 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/
最佳答案
无需添加多个图标。您可以通过创建变量current
和next
来控制播放
<!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的获取/设置currentTime “Radio Stations”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48245822/