[英]playing a randomly selected piece of audio from an array, cant figure out where I went wrong
the code is as follows: 代码如下:
var audio1 = new Audio("sounds/sleighride.mp3");
var audio2 = new Audio("sounds/letitsnow.mp3");
var audio3 = new Audio("sounds/comingtotown.mp3");
var array = ["audio1", "audio2", "audio3"]
function soundrandom (){
var songrandom = array[Math.floor(Math.random() * array.length)];
songrandom.play();
}
soundrandom();
the answer may be obvious but has had me stumped for a bit, error is "soundrandom.play(); is not a function". 答案可能很明显,但是让我有些困惑,错误是“ soundrandom.play();不是函数”。
They are objects not strings. 它们是对象而不是字符串。
var array = [audio1, audio2, audio3]
To make it easier to edit later you could always do this: 为了使以后的编辑更容易,您可以始终这样做:
var audioArr = [
new Audio("sounds/sleighride.mp3"),
new Audio("sounds/letitsnow.mp3"),
new Audio("sounds/comingtotown.mp3")
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.