How do I trigger sounds randomly in Adobe Flash?

Posted on 2008-06-12
Last Modified: 2013-11-11
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 :)
Question by:mjmusa
  • 4
  • 4
LVL 12

Expert Comment

ID: 21776355
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

LVL 12

Expert Comment

ID: 21776742
If needed, I can upload a example :)

Author Comment

ID: 21780014
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 :)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 21780063
and sorry but I didnt understand what you meant by modify "sPath" and "aSounds" to fit to your setup!
LVL 12

Accepted Solution

SamuelRostol earned 350 total points
ID: 21791353
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

Author Comment

ID: 21794063
YOU'RE A WIZARD :) I was wondering if I can change the sound variables to SWF files

Author Comment

ID: 21794209
or video clips..
LVL 12

Expert Comment

ID: 21800499
The principle can be used the same way, but the code must be modified for playing swf/video :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question