A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Random Sound Machine (AS3) Please help!!!

  1. #1
    Member
    Join Date
    May 2009
    Posts
    34

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

  2. #2
    Junior Member
    Join Date
    Nov 2008
    Posts
    4
    I am unfamiliar with AS3. To be honest I can't help, but I have a question which might help the overall thread in some way.

    My question is: How is Flash at handling Audio files?

    Proto, your scenario sounds alot like an idea I have for a sound "engine" app. I would like to play 1 short mp3 at a time from a database, array, "well" or whatever without any interruption, delay or pauses whatsoever.

    My "engine" is audio-intensive though. The "bank" of short audio clips needs to hold over 1000 audio pieces which can be choosen at random and played in sequence without any pause or delay.

    Is flash/AS3 a good platform for doing this? Or are there better choices out there for building an "App" like this, considering it must handle over 1000 short audio files with no timing flaws whatsoever.

    Thanks for your consideration.

  3. #3
    Member
    Join Date
    May 2009
    Posts
    34
    From my short experience with the Sound class in AS3, I'm not sure if it is the best choice for detailed and accurate sound handling/processing.

    It seems that you just want to play a simple mp3 player with a predetermined and randomizable playlist. Depending on the length of your sounds (less than 5 secs) there might be a space interval between clips. I can think off the top of my head some methods to bypass this but overall—and given your specific question—I don't know if I'd recommend you to go ahead because I don't know what other "choices" you have.

    Now, take all I say with a pinch of salt. I'm fairly new to AS3 myself and I only talk from my experience. I just figure it's better to have a reply from a newbie than no replies at all; support's what this board is about.

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center