How do I trigger sounds randomly in Adobe Flash?

Hi there,

I have 3 different sounds, I need an Actionscript that triggers them in random order when the FLash movie is started.

Thank you :)
Who is Participating?
SamuelRostolConnect With a Mentor Commented:
Hi there,

A zip with the entire project is attached. Check it out to see my solution.

What i meant by that, was that if you have different names on the folder, modify "sPath" to make sure you open sounds in the correct folder.
If you sounds have different names, modify the content of "aSounds" to make sure that the filenames are the same as in the array.

I hope the example helps you. You'll have to rename all the files - just take away the .txt from the ending of the filenames :)

Good luck!
| Samuel
Hi there,

Below I've attached the code that creates an array of sounds, finds one random sound, and plays it - and then moves on to the next sound. Place this on the first frame of your flash-movie, and run it :)

Remember to modify "sPath" and "aSounds" to fit to your setup!

Good luck!
// Folder that contains the sounds
var sPath :String = "sound/";
// Array that contains the different random sounds
var aSounds :Array = new Array("sound1.mp3", "sound2.mp3", "sound3.mp3");
// The channel that plays the sound and listens for the Complete-event
var Channel :SoundChannel = new SoundChannel();
// Initialising the application
// Function that finds which sound to play
function playSounds()
	// Finding url to random sound
	var iRandomSound :Number = Math.floor(Math.random() * aSounds.length);
	var sSoundUrl :String = sPath + aSounds[iRandomSound];
	// Remove the played sound from the array, so that the next time this
	// function is run, the sound that already has been played, does not play again
	// Creating and loading new sound-object
	var soundPlayer :Sound = new Sound();
	var soundRequest:URLRequest = new URLRequest(sSoundUrl);
	// Playing sound-object
	Channel =;
	Channel.addEventListener(Event.SOUND_COMPLETE, completeHandler);
// Function that runs the "next" sound.
function completeHandler(e:Event) {
	if (aSounds.length > 0) playSounds();

Open in new window

If needed, I can upload a example :)
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

mjmusaAuthor Commented:
Hello Samuel,

Thank you very much for your help, I copied the code into the first frame, saved it in a folder and put three different sound clips, but I didnt know what to do next because there were several errors, I would be grateful if you can upload an example.

Thx again :)
mjmusaAuthor Commented:
and sorry but I didnt understand what you meant by modify "sPath" and "aSounds" to fit to your setup!
mjmusaAuthor Commented:
YOU'RE A WIZARD :) I was wondering if I can change the sound variables to SWF files
mjmusaAuthor Commented:
or video clips..
The principle can be used the same way, but the code must be modified for playing swf/video :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.