简体   繁体   English

播放阵列中随机选择的音频,无法弄清楚我哪里出了错

[英]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.

相关问题 CSS-有人可以帮助我找出下拉菜单导航出现的问题吗? - CSS - Can someone help me figure out where I went wrong with my dropdown menu nav? 在进行计算时,如何获得某个值以对应另一个值? 以前有过,现在不知道哪里出错了 - How can I get a certain value to correspond to another when doing a calculation? I had it previously, now I can't figure out where I went wrong 我一生都无法找出这个脚本有什么问题 - I cant for the life of me figure out whats wrong with this script CSS-需要帮助找出下拉菜单导航出错的地方 - CSS - Need help figuring out where I went wrong with my dropdown menu nav 渲染一无所获,但我不知道为什么 - Nothing was returned from render but I cant figure out why 从数组JavaScript播放音频 - Playing audio from array JavaScript 我的桌子看起来很奇怪,无法弄清楚我在 javascript 上做错了什么 - my table is looking weird and cant figure out what i did wrong on the javascript 我试图为我的训练营做一个测验,但我不知道代码有什么问题 - im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code 无法弄清楚为什么我无法从我的 API 中检索数据 - Cant figure out why I cant retrieve data from my API 我不知道的滚动错误 - scrolling bug that i cant figure out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM