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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sound Randomizer - This time external 1

Status
Not open for further replies.

eladi

Programmer
Sep 4, 2001
80
0
0
AU
Hi all,

I posted a similar thread about a week ago. Tnx for your reply wangbar. But now I have to go a step further:

I had a mp3-Loop designed for my website. It was made in a way, that it could be split into 8 parts and played at random order. As even the parts are too big (for an analog modem), I want to load them externally. What I want is the following:

1. 1.mp3 is loaded. Once finished loading it will be played.
2. firstSound (1.mp3) will be played while 2.mp3 (secondSound) is loaded.
3. As soon as the secondSound is loaded, the two sounds will be played at random order, while the third loads...
4. As soon as the thirdSound is loaded, the three sounds will be played at random order, while the fourth loads...
5. - 9. According to step 3 and 4 until all 8 files are loaded.

The loading of the files seems to be ok. It loads firstSound, plays it until secondSound is loaded and then randomizes between the two. The problem is, as soon as secondSound is completed, it stops. The secondSound.onSoundComplete 'handler' seems not to be excecuted - although it was created with copy&paste from firstSound.onSoundComplete 'handler'. I stopped right here. It doesn't make sense developping on, while this problem
remains.

Here is how it looks right now and the fla-file to download.

I'd be greatful for help.
Adrian
 
The reason the second onSoundComplete isn't firing is because it's set up before you actually set up a sound object for it to attach to - the onComplete is in frame one but the sound object that it applies to isn't created until frame 4...

Try setting up all the sound objects in frame 1 before you create the functions which are dependant upon them. Also if you go back to the code I posted regarding your first problem you can easily adapt it to work here too so that you don't have all the repetition of switch statements.
 
wangbar,

The handler isn't activated, even if I define the secondSound object in the first frame. And I definitely thought about a way of putting your (much better) solution into it. But I can't see how that should be possible, as I have to load one after another and can not simply attach them like when I work with imported sounds.

Tnx.
Adrian
 
Posted this on the wrong thread...

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
soundFile = [];
loadedSound = [];
var numSounds = 4;

//load sounds
for (var i = 1; i<=numSounds; i++) {
soundFile = new Sound();
soundFile.onload = function() {
//assign sounds to second (playing) array as soon as they're fully loaded
loadedSound.push(soundFile);
};
soundFile.loadSound('sounds/sound'+i+'.mp3');
}

//generates a random number between 1 and whatever's loaded
function randomNum() {
return Math.floor(Math.random()*loadedSound.length)+1;
}

//main function

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

generateRandomSound();
};
randomSound.start();
}

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

stop();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top