Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sound Randomizer 1

Status
Not open for further replies.

eladi

Programmer
Sep 4, 2001
80
AU
Hi,

A friend composed some sound for my website. We split it into 11 pieces in a way that they can be played in random order. For now I'm working on the randomizer, therefore I have the sounds imported (I'll do the external loading later) and did the linking settings including id.

first: i read somewhere that i can create only 8 sound objects. is that ture? if so, i will have him got the piece into 8 parts not 11.

second: i think i got a way i can play them at random order now. but for some reason, there's an error. i trace the random numer and in which soundCompleted function it is generated. nevertheless, it always just plays track 7, although the trace comes from another 'soundCompleted handler'. here's the code. i know, this might be done more compact, but anyway:

firstSound = new Sound();
firstSound.attachSound("sound1");
secondSound = new Sound();
secondSound.attachSound("sound2");
thirdSound = new Sound();
thirdSound.attachSound("sound3");
forthSound = new Sound();
forthSound.attachSound("sound4");
fifthSound = new Sound();
fifthSound.attachSound("sound5");
sixthSound = new Sound();
sixthSound.attachSound("sound6");
seventhSound = new Sound();
seventhSound.attachSound("sound7");
eighthSound = new Sound();
eighthSound.attachSound("sound8");
firstSound.start(0,1);
firstSound.onSoundComplete = function() {
auswahl = (random(9));
trace("Von firstSound " + auswahl);
firstSound.stop();
switch(auswahl) {
case 1: firstSound.start();
case 2: secondSound.start();
case 3: thirdSound.start();
case 4: forthSound.start();
case 5: fifthSound.start();
case 6: sixthSound.start();
case 7: seventhSound.start();
case 8: eighthSound.start();
default: firstSound.start();
break;
}
}
secondSound.onSoundComplete = function() {
auswahl = (random(9));
trace("Von secondSound " + auswahl);
secondSound.stop();
switch(auswahl) {
case 1: firstSound.start();
case 2: secondSound.start();
case 3: thirdSound.start();
case 4: forthSound.start();
case 5: fifthSound.start();
case 6: sixthSound.start();
case 7: seventhSound.start();
case 8: eighthSound.start();
default: firstSound.start();
break;
}
}

.
.
.
it continues on until eighthSound similarly. The code is copy&pasted. I also checket the .Sound.stop() functions, especially in seventhSound, but it seems to be ok. here:

seventhSound.onSoundComplete = function() {
auswahl = (random(9));
trace("Von seventhSound " + auswahl);
seventhSound.stop();
switch(auswahl) {
case 1: firstSound.start();
case 2: secondSound.start();
case 3: thirdSound.start();
case 4: forthSound.start();
case 5: fifthSound.start();
case 6: sixthSound.start();
case 7: seventhSound.start();
case 8: eighthSound.start();
default: firstSound.start();
break;
}
}

Would be greatful for help.
Adrian
 
You start the first sound... and stop it to randomly set up on a second sound to be played, but you never seem to be starting that randomly selected sound? Regards,

oldman3.gif
 
How about this? Much shorter....

//set up sounds in an array for convenience
Code:
soundFile = [];
for (var i = 1; i<9; i++) {
	soundFile[i] = new Sound();
	soundFile[i].attachSound('soundFile'+i);
}
//generates a random number between 1 and 8
Code:
function randomNum() {
	return Math.floor(Math.random()*8)+1;
}
//main function
Code:
function generateRandomSound() {
	randomSound = soundFile[randomNum()];
	randomSound.onSoundComplete = function() {
//loop
Code:
generateRandomSound();
	};
	randomSound.start();
}
//go
Code:
generateRandomSound();

stop();
 
This works with files on my hard drive but I haven't tested it online - the only drawback is that you don't know exactly what order the sounds will load in.

//set up sounds in arrays
Code:
soundFile = [];
loadedSound = [];
var numSounds = 4;
//load sounds
Code:
for (var i = 1; i<=numSounds; i++) {
	soundFile[i] = new Sound();
	soundFile[i].onload = function() {
//assign sounds to second (playing) array as soon as they're fully loaded
Code:
loadedSound.push(soundFile[i]);
	};
	soundFile[i].loadSound('sounds/sound'+i+'.mp3');
}
//generates a random number between 1 and whatever's loaded
Code:
function randomNum() {
	return Math.floor(Math.random()*loadedSound.length)+1;
}
//main function

Code:
function generateRandomSound() {
//select from sounds which have loaded
Code:
randomSound = loadedSound[randomNum()-1];
	randomSound.onSoundComplete = function() {
//loop to new sound

Code:
generateRandomSound();
	};
	randomSound.start();
}
//go
Code:
this.onEnterFrame = function() {
	if (loadedSound.length>0) {
//wait for the first sound to load then start looping through them
Code:
generateRandomSound();
		delete this.onEnterFrame;
	}
};

stop();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top