Solved

How do I trigger sounds randomly in Adobe Flash?

Posted on 2008-06-12
8
168 Views
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 :)
0
Comment
Question by:mjmusa
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:SamuelRostol
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!
Kindly,
Samuel
// 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

playSounds();
 
 

// 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

	aSounds.splice(iRandomSound,1);

	

	// Creating and loading new sound-object

	var soundPlayer :Sound = new Sound();

	var soundRequest:URLRequest = new URLRequest(sSoundUrl);

	soundPlayer.load(soundRequest);

	

	// Playing sound-object

	Channel = soundPlayer.play();

	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

0
 
LVL 12

Expert Comment

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

Author Comment

by:mjmusa
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 :)
0
 

Author Comment

by:mjmusa
ID: 21780063
and sorry but I didnt understand what you meant by modify "sPath" and "aSounds" to fit to your setup!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Accepted Solution

by:
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
RandomSound.zip
0
 

Author Comment

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

Author Comment

by:mjmusa
ID: 21794209
or video clips..
0
 
LVL 12

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now