HTML/JS音频播放列表在页面退出时暂停/恢复



我不确定要求我做的事情是否可行,我在JS方面仍然没有太多专业知识,所以我一直在谷歌上搜索,但到目前为止还没有找到。

我们正在使用一个名为RiseVision的程序来运行演示,它基本上是自动化的;增强Powerpoint。所以我们有一个幻灯片,我设法在播放列表的背景中播放音乐,然而,有时我们会将这些演示设置为退出,然后切换到youtube视频,然后重新开始演示。我被要求做的是,一旦幻灯片在视频后再次启动,让音乐从原来的位置恢复,或者至少跳到下一首歌。

由于RiseVision程序的性质,我只能操作

音乐在隐藏在幻灯片后面的HTML小部件上运行,视频播放时该小部件将关闭,然后再次打开。

由于它只是一个小部件,我没有能力操作文件结构,所以我只是将JS粘贴在标签中

这些显示器上没有用户输入,所以一切都会自动运行,我们可以远程更新演示文稿。

任何想法都将不胜感激,我在想也许我可以使用eventlistener来停止并以某种方式保存一个索引

这是我正在使用的代码,几乎取自这个视频:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Audio Player</title>
<style>
#playlist{
list-style: none;
}
#playlist li a{
color:black;
text-decoration: none;
}
#playlist .current-song a{
color:blue;
}
</style>
<!-- Source Code From YouTube.com/MicroTechTutorials  you may remove this message on your webpage but please do not redistribtue -->
</head>
<body>
<audio src="" controls id="audioPlayer">
Sorry, your browser doesn't support html5!
</audio>
<ul id="playlist">
<li class="current-song">
<a href="Paralyzer.m4a">Paralyzer</a></li>
<li><a href="Minority.m4a">Minority</a></li>
<li><a href="YoureGonnaGoFarKid.m4a">You're Gonna Go Far, Kid</a></li>
</ul>
<script src="https://code.jquery.com/jquery-2.2.0.js"></script>
<script language="javascript">
function audioPlayer(){
var currentSong = 0;
$("#audioPlayer")[0].src = $("#playlist li a")[0];
$("#audioPlayer")[0].play();
$("#playlist li a").click(function(e){
e.preventDefault(); 
$("#audioPlayer")[0].src = this;
$("#audioPlayer")[0].play();
$("#playlist li").removeClass("current-song");
currentSong = $(this).parent().index();
$(this).parent().addClass("current-song");
});
$("#audioPlayer")[0].addEventListener("ended", function(){
currentSong++;
if(currentSong == $("#playlist li a").length)
currentSong = 0;
$("#playlist li").removeClass("current-song");
$("#playlist li:eq("+currentSong+")").addClass("current-song");
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
});
}
</script>
<script>
// loads the audio player
audioPlayer();
</script>
</body>
</html>

最终实现了这一点,答案实际上比我尝试的要简单得多。我可以使用localStorage保存当前歌曲的索引,并在软件恢复时加载列表中的下一首歌曲。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Audio Player</title>
<style>
#playlist{
list-style: none;
}
#playlist li a{
color:black;
text-decoration: none;
}
#playlist .current-song a{
color:blue;
}
</style>
</head>
<body>
<!--Audio Player--> 
<audio src="" controls id="audioPlayer">
Sorry, your browser doesn't support html5!
</audio>
<!-- List of songs -->
<ul id="playlist">
<li class="current-song">
<a href="[Link to Song Here]">[Name of Song Here]</a></li>
<li><a href="[Link to Song Here]">[Name of Song Here]</a></li>
<li><a href="[Link to Song Here]">[Name of Song Here]</a></li>
</ul>
<script src="https://code.jquery.com/jquery-2.2.0.js"></script>
<script language="javascript">
function audioPlayer(){
var currentSong = 0;
//If the locally stored position doesn't exist yet, set it to currentSong, otherwise increment it by one
if (localStorage.songPosition === "NaN"){
localStorage.songPosition = currentSong
}
else{
localStorage.songPosition++
}
//If the stored song position is greater than the length of the song index, reset it.
if(localStorage.songPosition > $("#playlist li a").length-1)
localStorage.songPosition = 0;
//Play the song at the locally stored position
$("#audioPlayer")[0].src = $("#playlist li a")[localStorage.songPosition];
$("#audioPlayer")[0].play();
//Allows you to click titles to change songs, this is mostly for an actual web page and not necessary for an automatic system.
$("#playlist li a").click(function(e){
e.preventDefault(); 
$("#audioPlayer")[0].src = this;
$("#audioPlayer")[0].play();
$("#playlist li").removeClass("current-song");
currentSong = $(localStorage.songPosition).parent().index();
$(this).parent().addClass("current-song");
});
//Check for the end of the current song, increment the current song counter and play the next one.
$("#audioPlayer")[0].addEventListener("ended", function(){
currentSong++;
localStorage.songPosition = currentSong;
if(currentSong == $("#playlist li a").length)
currentSong = 0;
$("#playlist li").removeClass("current-song");
$("#playlist li:eq("+currentSong+")").addClass("current-song");
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
});
}
</script>
<script>
// loads the audio player
audioPlayer();
</script>

最新更新