简体   繁体   English

对于一系列图片,我希望每个图像都有不同的音频

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

相关问题 我有一个选项表单,我想为我选择的选项标签的每个元素显示来自 JSON 文件的不同图像 - I have an option form and I would like to show a different image from a JSON file for each element of the option tag i choose 我有3个div,每个div都有自己的主题,现在我希望每个div打开其模式 - I have 3 divs each with it's own topic, and now I would like for each div to open its modal 我有一个JavaScript数组,我想下载数组中的每个图像 - I have a JavaScript array and I want to download each image in the array 在 Adob​​e Acrobat 中,我想要四个单选按钮,每个按钮都会在文本字段中添加/替换文本 - In Adobe Acrobat I would like to have four radio buttons, each of which will add/replace text into a text field 你好,我想要一个不同的随机数,但我总是得到相同的,一个想法? - hello, I would like to have a different random number, but I always get the same, an idea? 我如何让用户上传多张图片(例如facebook)? - How can I have users upload multiple pictures like facebook? 当我单击编辑图像时,我希望我的编辑功能能够正常工作 - I would like to have my edit function to work when i click on the edit image 我想为主角添加一个新的口袋妖怪图像,以便在每次成功的驼峰后跳过 - I would like to add a NEW Pokemon image for the main character to jump over after each successful hump 我想让一个javascript数组成为条件 - I would like to make a javascript array conditional 我使用javascript / firebase构建和应用程序,并希望它返回最近5个搜索并使每个搜索都可点击 - I've built and app using javascript /firebase and would like to have it return the last 5 searches and make each of them clickable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM