gpt4 book ai didi

javascript - 根据随机数组结果播放特定的声音?

转载 作者:行者123 更新时间:2023-12-03 02:08:33 24 4
gpt4 key购买 nike

我正在使用2个充满单词的数组在javascript中制作一个“用户名”生成器,然后使用随机化从每个数组中显示1个单词。但是,假设我要在第一个数组中选择第二个单词时播放标记为“1.mp3”的特定声音,并在第二个数组中选择第九个单词时播放标记为“8.mp3”的声音。这些声音将在页面加载时按第一个数组声音然后第二个数组声音的顺序播放。我正在使用cordova的Media插件获得电话支持。有人能指出我正确的方向吗?

Javascript数组如下所示:

var wordlist1 = ["Aggressive", "Amazing", "Attractive", "Black", "Broken", "Brave",
"Caring", "Cold", "Corny", "Delicious", "Dramatic", "Dull", "Euphoric", "Exotic",
"Evil", "Fabulous", "Feminine", "Fat", "Glorious", "Gripping", "Grand", "Hairy",
"Horny", "Hard", "Immense", "Idiotic", "Inferior", "Jam-Packed", "Juicy", "Jealous",
"Kooky", "Klutzy", "Knowledgeable", "Lame", "Lazy", "Loving", "Mad", "Milky", "Moist",
"Naive", "Naughty", "Neglected", "Oily", "Obese", "Overcooked", "Passionate", "Party",
"Peaceful", "Pasty", "Questionable", "Queasy", "Qualified", "Reckless", "Repulsive",
"Rubbery", "Slimy", "Salty", "Stiff", "Thick", "Terrible", "Thunderous", "Ugly",
"Uncomfortable", "Unpleasant", "Violent", "Vivid", "Vicious", "Wobbly", "Wee", "Wet",
"Young", "Yummy", "Yellowish", "Zany", "Zappy", "Zealous"]
var displayword1 = wordlist1[Math.floor(Math.random() * wordlist1.length)];
document.getElementById("word1").innerHTML = displayword1

第二个数组几乎相同,除了数组和名称的内容
然后,我有了一个HTML文档,它将加载脚本。
<p id="word1"></p>
<script src="main.js"></script>

最佳答案

var wordlist1 = ["Aggressive", "Amazing", "Attractive", "Black", "Broken", "Brave",
"Caring", "Cold", "Corny"]


var randomNumber = Math.floor(Math.random() * wordlist1.length);
var displayword1 = wordlist1[randomNumber];
document.getElementById("word1").innerHTML = displayword1
var media = new Media(randomNumber+".mp3", function(){/*success*/}, function(){/*error*/});
media.play();

也许您需要将成功回调更改为调用this.play()的回调。

关于javascript - 根据随机数组结果播放特定的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com