如何在我的博客页面上一个接一个地播放随机音乐曲目?
How do I play a random music track one after the other on my blogger page?
我希望我的博主页面播放随机音乐曲目,当一首曲目结束时播放另一首随机选择的音乐曲目。
这是我尝试过的:
<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>
<script type="text/javascript">
function play(){
var a = Math.random()*3;
a=Math.floor(a);
if(a==0)
{document.getElementById('soundtrack').innerHTML="<audio autoplay><source src='https://archive.org/download/lohkungz_s0001/lohkungz_s0001.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>";}
var aud = document.getElementById('soundtrack').getElementsByTagName('audio');
aud[0].onended = function() {
play();
};
}
play();
</script>
你可以看看下面的代码,可能对你有帮助。其示例代码,您可以添加正确的mp3文件并使用。
musics = ['https://archive.org/download/lohkungz_s0001/lohkungz_s0001.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0002.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0003.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0004.mp3'];
var aud = document.getElementById("music_palyer");
aud.onended = function() {
console.log("The audio has ended");
var a = Math.random()*musics.length;
a=Math.floor(a);
aud.src=musics[a];
console.log(aud.src);
aud.load();
<div id="soundtrack">
<audio id="music_palyer" autoplay controls="controls"><source src='https://archive.org/download/lohkungz_s0001/lohkungz_s0001.mp3' type='audio/mp3'>Your browser does not support the audio element.</audio>
</div>
您应该使用 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>
我希望我的博主页面播放随机音乐曲目,当一首曲目结束时播放另一首随机选择的音乐曲目。
这是我尝试过的:
<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>
<script type="text/javascript">
function play(){
var a = Math.random()*3;
a=Math.floor(a);
if(a==0)
{document.getElementById('soundtrack').innerHTML="<audio autoplay><source src='https://archive.org/download/lohkungz_s0001/lohkungz_s0001.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>";}
var aud = document.getElementById('soundtrack').getElementsByTagName('audio');
aud[0].onended = function() {
play();
};
}
play();
</script>
你可以看看下面的代码,可能对你有帮助。其示例代码,您可以添加正确的mp3文件并使用。
musics = ['https://archive.org/download/lohkungz_s0001/lohkungz_s0001.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0002.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0003.mp3','https://archive.org/download/lohkungz_s0001/lohkungz_s0004.mp3'];
var aud = document.getElementById("music_palyer");
aud.onended = function() {
console.log("The audio has ended");
var a = Math.random()*musics.length;
a=Math.floor(a);
aud.src=musics[a];
console.log(aud.src);
aud.load();
<div id="soundtrack">
<audio id="music_palyer" autoplay controls="controls"><source src='https://archive.org/download/lohkungz_s0001/lohkungz_s0001.mp3' type='audio/mp3'>Your browser does not support the audio element.</audio>
</div>
您应该使用 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>