gpt4 book ai didi

javascript - 如何在我的博客页面上一个接一个地播放随机音乐轨道?

转载 作者:行者123 更新时间:2023-11-28 04:57:00 24 4
gpt4 key购买 nike

我希望我的博客页面播放随机音乐轨道,当轨道结束时,后面会播放另一个随机选择的音乐轨道。

这是我尝试过的:

<html>
<body>
<div id="soundtrack"></div>

<script type="text/javascript">
var a = Math.random()*3;
a = Math.floor(a);

if(a==0)
{
document.getElementById('soundtrack')
.innerHTML="<audio autoplay><source src='s0000.mp3' type='audio/mp3'>Your browser does not support the audio element.</audio>";
}
if(a==1)
{
alert(a);
document.getElementById('soundtrack')
.innerHTML="<audio autoplay><source src='s0001.mp3' type='audio/mp3'>Your browser does not support the audio element.</audio>";
}
if(a==2)
{
document.getElementById('soundtrack')
.innerHTML="<audio autoplay><source src='s0002.mp3' type='audio/mp3'>Your browser does not support the audio element.</audio>";
}

</script>
</body>
</html>

最佳答案

您应该使用 ended音频事件处理程序

试试这个。

<html>
<body>
Random Audio Demo
<div >
<audio id="soundtrack" autoplay>
<source src='https://archive.org/download/kkkfffbird_yahoo_Beep_201607/beep.mp3' type='audio/mp3'>
Your browser does not support the audio element.</audio>

</div>

<script type="text/javascript">

var audio = document.getElementById('soundtrack'),
music = [
'https://archive.org/download/kkkfffbird_yahoo_Beep_201607/beep.mp3',
'https://archive.org/download/Beep_7/beep.mp3',
'https://archive.org/download/beep-07/beep-07.mp3'
];

audio.addEventListener('ended', function(e) {
var r = Math.floor(Math.random()*3);

audio.src = music[r];

});

</script>
</body>
</html>

关于javascript - 如何在我的博客页面上一个接一个地播放随机音乐轨道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42468004/

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