Random Sound Machine (AS3) Please help!!!
Hi, I've already posted two times something similar to this on another part of this forum and I got no replies. I figured I should give it a last try here, as this is making me nuts!!!!
I want to make a sound machine that selects 5 random mp3's from a total of 23 stored in an array. After they've been loaded and selected they should be played once at a start time randomly selected. So far so good, but now I want to detect whenever a piece has stopped playing so I can select another start time and playing it again. The idea behind this is to give the impression of continuity without an obvious manipulation of the sounds and without having voids or mute spaces between each play....
Please help me!
Code:
var START_PLAY:String="startPlay";
var TOGGLE:String="toggle";
var s1:Sound=new Sound(),s2:Sound=new Sound(),s3:Sound=new Sound(),s4:Sound=new Sound(),s5:Sound=new Sound();
var sc1:SoundChannel=new SoundChannel(),sc2:SoundChannel=new SoundChannel();
var sc3:SoundChannel=new SoundChannel(),sc4:SoundChannel=new SoundChannel(),sc5:SoundChannel=new SoundChannel();
var sound:Array=new Array(s1,s2,s3,s4,s5);
var soundchannel:Array=new Array(sc1,sc2,sc3,sc4,sc5);
var audiobuffer:Array=new Array();
var urlR:URLRequest=new URLRequest();
var count:int=0;
var togglecount:int=5;
var nummp3s:int=25;
addEventListener(START_PLAY, startPlayListener);
for (var i:int=1; i<=nummp3s; i++) {
audiobuffer.push(new URLRequest("zzz"+i+".mp3"));
}
for each (var audio:Sound in sound) {
urlR=audiobuffer[Math.ceil(Math.random()*nummp3s)];
audio.load(urlR);
audio.addEventListener(Event.COMPLETE, completeListener);
}
//this one checks if all the sounds have been loaded...
function completeListener(event:Event):void {
if (count!=5) {
count++;
dispatchEvent(new Event(START_PLAY));
dispatchEvent(new Event(TOGGLE));
}
}
//this one is supposed to toggle the boolean soundOff to true whenever it detects no sound
//is playing on the stage. If no sound is played it calls the soundOffListener...
function toggleListener(event:Event):void {
if (togglecount==0) {
soundOffListener();
trace("sound is off");
} else if (togglecount!=0) {
trace("togglecount: "+ togglecount);
togglecount--;
}
}
function soundOffListener():void {
var randomsound:int=Math.floor(Math.random()*sound.length);
sound[randomsound].play();
sound[randomsound].addEventListener(TOGGLE, toggleListener);
trace("www ccccmmmm");
}
//this one is the sound play starter.
function startPlayListener(event:Event):void {
if (count==5) {
for (var c:int=0; c<5; c++) {
var starts:Number=Math.ceil(Math.random()*sound.length);
soundchannel[c]=sound[c].play(starts);
soundchannel[c].addEventListener(TOGGLE, toggleListener);
soundchannel[c].addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
trace(sound[c]);
}
}
}
//this one is supposed to check what soundchannel has completed and on that account...
//play the sound again from a new startpoint (variable "startat").
//this startpoint variable is intended to be different for each soundCompleteHandler call.
function soundCompleteHandler(event:Event):void {
var startsat:Number=Math.ceil(Math.random()*sound.length);
if (sc1) {
trace("1");
sc1.stop();
sc1=sound[0].play(startsat);
} else if (sc2) {
trace("2");
sc2.stop();
sc2=sound[1].play(startsat);
} else if (sc3) {
trace("3");
sc3.stop();
sc3=sound[2].play(startsat);
} else if (sc4) {
trace("4");
sc4.stop();
sc4=sound[3].play(startsat);
} else if (sc5) {
trace("5");
sc5.stop();
sc5=sound[4].play(startsat);
}
}