A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: random sound script

  1. #1
    Senior Member
    Join Date
    Mar 2003
    Posts
    316

    random sound script

    Hi
    I was hoping if someone could help me write script in F8 that
    would play sound on random basis with a fadein and fade out.

    Something like people talking in a restaurant which would be too much
    as constant ambient noise .. but it would rather from time to time
    play in the background ... so that I could mix in few sound clips playing randomly and using small clips I reduce size and make more exciting effect...

    any ideas ?

    thanks

  2. #2
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    stuff all of your sounds into an array.. then just call a random sound with setInterval()... you can even set it up to call a random sound, at a random interval of time.... maybe something like this (and i'm just guessing here.. i have no ideas how to stick sounds into an array...):
    Code:
    var sound1 = new Sound();
    sound1.attachSound("sound01_ID");
    var sound2 = new Sound();
    sound2.attachSound("sound02_ID");
    var sound3 = new Sound();
    sound3.attachSound("sound03_ID");
    
    var mySounds:Array = [sound1, sound2, sound3];
    
    function randomSound(time) {
    	wait = time*1000;  // time in seconds...
    	go = function () {
    		clearInterval(waiting);
    		this[random(mySounds[mySounds.length])].start(0, 1);
    	};
    	var waiting = setInterval(go, wait);
    }
    
    randomSound(5);  // 5 = 5 seconds
    maybe that'll give you something to work from at least..
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  3. #3
    Senior Member
    Join Date
    Mar 2003
    Posts
    316
    I couldn't get this to work at all ?

  4. #4
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    WEll, i didn't say it work, i said it would give you a place to work from...
    But, maybe you want to try this... when i want to use random elements from an array, this is how i do it (and this works just fine with sounds.. i tested it):
    Code:
    // invoking this method will get random elements of the called array
    
    Array.prototype.randomArray = function() {
    	var len = this.length;
    	var temp = new Array();
    	for (var i = 0; i<len; i++) {
    		var ran = Math.round(Math.random()*(this.length-1));
    		temp[i] = this[ran];
    		this.splice(ran, 1);
    	}
    	for (var j = 0; j<len; j++) {
    		this[j] = temp[j];
    	}
    };
    
    /*
    // ------------- EXAMPLE USAGE ------------ >>
    var myArray:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    trace(myArray);
    myArray.randomArray();
    trace(myArray);
    */
    
    
    // ---------- FOR YOUR SOUNDS --------->>
    
    // assuming you are creating these sound objects on your _root timeline...
    var sound0 = new Sound();
    sound0.attachSound("sound00_ID");
    sound0.stop();
    
    var sound1 = new Sound();
    sound1.attachSound("sound01_ID");
    sound1.stop();
    
    var sound2 = new Sound();
    sound2.attachSound("sound02_ID");
    sound2.stop();
    
    var mySounds:Array = [sound0, sound1, sound2];
    
    // This is the function to randomize the sound array, and play another sound
    function randomSound(time) {
    	// time in seconds...
    	wait = time*1000;
    	go = function () {
    		stopAllSounds();
    		_root.mySounds.randomArray();
    		clearInterval(waiting);
    		mySounds[0].start(0, 1);
    		// call the function again to keep it looping
    		randomSound(5);
    	};
    	waiting = setInterval(go, wait);
    }
    
    
    // start things off...
    
    mySounds[0].start(0, 1);
    randomSound(5);   // 5 = 5 seconds
    
    stop();
    so there you go.... you'll need to change the linkage ID's in my example to match yours, and adjust the time interval to the number of seconds you want to wait. But otherwise, you shouldn't need to change a thing.
    Last edited by madzigian; 07-21-2006 at 01:01 PM.
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

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