You can handle this within Javascript by creating a "player" element that will be used to play all of your sounds (an <embed> element will be placed within the element to play the sound) :
<span id='player'></span>
and then you will need a Javascript function to select a random sound to play and to play it :
//This will play a random sound
function playRandomSound(){
//An array to house all of the URLs of your sounds
var sounds = [ "http://www.mysite.com/1.wav",
"http://www.mysite.com/2.wav",
"http://www.mysite.com/3.wav",
"http://www.mysite.com/4.wav",
"http://www.mysite.com/5.wav",
"http://www.mysite.com/6.wav",
"http://www.mysite.com/7.wav",
"http://www.mysite.com/8.wav"];
//This line will select a random sound to play out of your provided URLS
var soundFile = sounds[Math.floor(Math.random()*sounds.length)];
//Find the player element that you created and generate an embed file to play the sound within it
document.getElementById("player").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
You can use a simple button onclick event to call this :
<input type='button' onclick='playRandomSound();' value='Play Random Sound' />
You may want to try this example, which should work in IE (Chrome may prompt you to download files, however it would be possible to implement an HTML5 solution for that):
SupernovaX36...
Member
2 Points
16 Posts
Making a random sound play when a button is clicked
Jan 18, 2013 12:26 PM|LINK
.
Rion William...
All-Star
27906 Points
4618 Posts
Re: Making a random sound play when a button is clicked
Jan 18, 2013 12:38 PM|LINK
The following StackOverflow thread discusses how to implement playing a sound when performing a click :
Playing a Random Sound When A Button Is Clicked
You can handle this within Javascript by creating a "player" element that will be used to play all of your sounds (an <embed> element will be placed within the element to play the sound) :
and then you will need a Javascript function to select a random sound to play and to play it :
//This will play a random sound function playRandomSound(){ //An array to house all of the URLs of your sounds var sounds = [ "http://www.mysite.com/1.wav", "http://www.mysite.com/2.wav", "http://www.mysite.com/3.wav", "http://www.mysite.com/4.wav", "http://www.mysite.com/5.wav", "http://www.mysite.com/6.wav", "http://www.mysite.com/7.wav", "http://www.mysite.com/8.wav"]; //This line will select a random sound to play out of your provided URLS var soundFile = sounds[Math.floor(Math.random()*sounds.length)]; //Find the player element that you created and generate an embed file to play the sound within it document.getElementById("player").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; }You can use a simple button onclick event to call this :
SupernovaX36...
Member
2 Points
16 Posts
Re: Making a random sound play when a button is clicked
Jan 18, 2013 01:09 PM|LINK
I put this code in to my program but it didn't work.
I obviously edited it so that my actual sound links were in there, and I also tried directing putting in the name of the button, but it didn't work.
<input id="btnQuestion" input type='button' onclick='playRandomSound();' value='Play Random Sound' />
Not sure what I'm doing wrong.
Rion William...
All-Star
27906 Points
4618 Posts
Re: Making a random sound play when a button is clicked
Jan 18, 2013 01:19 PM|LINK
You may want to try this example, which should work in IE (Chrome may prompt you to download files, however it would be possible to implement an HTML5 solution for that):
Example
function playRandomSound(){ var sounds = [ "http://www.soundjay.com/button/button-1.wav", "http://www.soundjay.com/button/button-2.wav", "http://www.soundjay.com/button/button-3.wav", "http://www.soundjay.com/button/button-4.wav", "http://www.soundjay.com/button/button-5.wav", "http://www.soundjay.com/button/button-6.wav", "http://www.soundjay.com/button/button-7.wav", "http://www.soundjay.com/button/button-8.wav"]; var soundFile = sounds[Math.floor(Math.random()*sounds.length)]; document.getElementById("player").innerHTML="<embed src=\""+soundFile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; }along with :