当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Playing a specific sound according to random array outcome?

问题描述:

I am making a 'Username' generator in javascript using 2 arrays filled with words, then using randomization to display 1 word from each array. However, say I want to play a specific sound labelled '1.mp3' when the second word gets chosen in the first array, and play a sound labelled '8.mp3' when the ninth word gets chosen in the second array. These sounds would play on page load in order of first array sound, then second array sound. I'm using cordova's Media plugin for phone support. Can anybody point me in the right direction?

The Javascript array looks like this:

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

the second array is nearly identical save for contents of array & name

I then have an HTML document that will load the script;

<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();

maybe you will need to change the success callback to be the one calling this.play();

分享给朋友:
您可能感兴趣的文章:
随机阅读: