[英]With an array of pictures I would like to have a different audio for each image
I have set up a random slideshow that goes through many picture. 我已经建立了一张随机的幻灯片,可以播放许多图片。 I am wanting to add a short audio clip to each one and have them sync with the array I use for the random pictures.
我想为每个片段添加一个简短的音频剪辑,并使它们与我用于随机图片的数组同步。 Below is a similar example of what I have.
以下是我所拥有的类似示例。 I'm new to JavaScript, and I've searched hundreds of places for an answer but nothing seems to work.
我是JavaScript的新手,我已经在数百个地方搜索了答案,但似乎没有任何效果。 Please help.
请帮忙。 Thank you.
谢谢。
UPDATE. UPDATE。 I may not have been clear.
我可能不清楚。 This is what I am trying to do: My son loves animals.
这就是我想要做的:我的儿子爱动物。 He's autistic, so he is constantly listing out his animals and things about them.
他自闭症,所以他不断地列出自己的动物及其有关的事物。 I created a slideshow for his computer with all 500 pictures and stuff about them.
我为他的计算机创建了一个幻灯片演示,其中包含所有500张图片及其相关内容。 It auto plays random images on one screen and he can manually browse on another.
它会在一个屏幕上自动播放随机图像,而他可以在另一个屏幕上手动浏览。 He wants to record short, 5-10 second audio with their name and something about them.
他想用他们的名字和关于他们的东西录制5-10秒的短音频。 I want to be able to associate his recordings with the pictures and have the recordings play each time an image changes
我希望能够将他的录音与图片关联起来,并在每次图像更改时播放录音
addImages = new Array
addImages[0] = "one.bmp"
addImages[1] = "two.bmp"
addImages[2] = "three.bmp"
addImages[3] = "four.bmp"
addImages[4] = "five.bmp"
addImages[5] = "six.bmp"
addImages[6] = "seven.bmp"
imgCt = addImages.length
firstTime = true
addSound = new Array
addSound[0] = "tada.wav"
addSound[1] = "recycle.wav"
addSound[2] = "ringout.wav"
addSound[3] = "notify.wav"
addSound[4] = "tada.wav"
addSound[5] = "tada.wav"
addSound[6] = "tada.wav"
imgCt2 = addSound.length
function rotate()
{
if (document.images)
{
if (firstTime)
{
thisAdd = Math.floor((Math.random() * imgCt))
firstTime = false
}
else
{
thisAdd++
if (thisAdd == imgCt)
{
thisAdd = 0
}
}
document.myPicture.src=addImages[thisAdd]
setTimeout("rotate()", 3 * 1000)
}
}
You're not adding to an array properly. 您没有正确添加到数组。 You create an array then keep overwriting it;
创建一个数组,然后继续覆盖它; instead of setting the variable over and over, use
push()
不用一遍又一遍地设置变量,而使用
push()
addImages = new Array
addImages.push(["one.bmp", "tada.wav"])
addImages.push(["two.bmp", "tada.wav"])
addImages.push(["three.bmp", "tada.wav"])
addImages.push(["four.bmp", "tada.wav"])
addImages.push(["five.bmp", "tada.wav"])
addImages.push(["six.bmp", "tada.wav"])
addImages.push(["seven.bmp", "tada.wav"])
To get an image you'd do addImages[i][0]
, to get an image you do addImages[i][1]
. 要获取图像,请执行
addImages[i][0]
,要获取图像,请执行addImages[i][1]
。 There's no need to make a separate array for sounds like you do after. 无需像您以后那样为声音创建单独的数组。
Thank you everyone for your assistance. 谢谢大家的帮助。 I was able to come up with an answer.
我能够提出一个答案。
<script>
function pickimg() {
var imagenumber = 4;
var randomnumber = Math.random();
var rand1 = Math.round((imagenumber - 1) * randomnumber) + 1;
images = new Array
images[1] = "image1.bmp"
images[2] = "image2.bmp"
images[3] = "image3.bmp"
images[4] = "image4.bmp"
var image = images[rand1];
document.randimg.src = image
var sounds = new Array
sounds[1] = "sound1.wav"
sounds[2] = "sound2.wav"
sounds[3] = "sound3.wav"
sounds[4] = "sound4.wav"
var sound = sounds[rand1];
audioTag.src = sound;
audioTag.play();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.