我试图制作一系列声音文件,这些声音文件将以随机顺序播放但不确定......是否可以制作音频文件和数组?
也许是这样的?
var audio = new Audio ["christmas.mp3","jingleBells.mp3"]
audio.play()
答案 0 :(得分:1)
当然可以
var audio_files = [
"christmas.mp3",
"jingleBells.mp3"
]
var random_file = audio_files[Math.floor(Math.random() * audio_files.length)];
var audio = new Audio(random_file);
audio.play();
答案 1 :(得分:0)
试试这个
var musicFiles = ["play1","play2","play3","play4"];
function play(randomFile){
return randomFile[Math.floor(Math.random() * (randomFile.length ))];
}
play(musicFiles);
答案 2 :(得分:0)
感谢adeneo对这个帖子的回复,我使用了代码,这很有帮助!
我想做一点改进,我注意到它没有足够随机化。
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function audioplay1(){
var audio_files = ["audiofiles/emotions/afraid.mp3", "audiofiles/emotions/anger.mp3", "audiofiles/emotions/disgust.mp3", "audiofiles/emotions/happy.mp3", "audiofiles/emotions/sad.mp3", "audiofiles/emotions/surprised.mp3", "audiofiles/emotions/disgust.mp3"];
var random_file = audio_files[getRandomInt(0,audio_files.length)];
var audio = new Audio(random_file);
audio.play();
}