JQuery 获取/设置当前时间“广播电台”
Posted
技术标签:
【中文标题】JQuery 获取/设置当前时间“广播电台”【英文标题】:JQuery Get/Set currentTime "Radio Stations" 【发布时间】:2018-01-14 01:06:02 【问题描述】:我想要达到的效果是悬停在某些图标上会播放某些音频文件。当您切换图标时,新音频会从最后一个音频停止的确切时间开始播放 - 这应该就像您正在更改收音机上的频道一样。
下面的代码有效,但仅在两个音频文件之间有效,并且它们绑定到同一个按钮。
我怎样才能复制这种效果,但有 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/
【问题讨论】:
【参考方案1】:无需添加多图标。您可以通过创建变量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>
【讨论】:
这似乎没有从上次停止的地方获取/设置选定的音频播放?还有多个图标,用户可以选择他们的频道。以上是关于JQuery 获取/设置当前时间“广播电台”的主要内容,如果未能解决你的问题,请参考以下文章