Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play sound from library in AS3?

In Flash 10/AS3, I added some sound and it seems to be working alright, but I think I'm doing it wrong. I imported the sound into the library, but I believe that it's reloading it from the folder with the swf/sound. I'm loading them like so:

var request1:URLRequest = new URLRequest("CLICK8C.mp3");
clickSound = new Sound();
clickSound.addEventListener(Event.COMPLETE, completeHandler);
clickSound.load(request1);

Is there a way to get it to just load it from the library?

like image 995
Ullallulloo Avatar asked Apr 23 '10 21:04

Ullallulloo


2 Answers

You need to make the sound in the library available to actionscript. After that, you can implement the sound object like any other class.

To make a library object available for actionscript, left click the item in the library and select 'Linkage'. Check the box next to 'Export for ActionScript'. You'll need to then give the object a class name and since you are dealing with a sound, make sure the base class is a Sound object.

Let's say you named your sound class "MySound", you can now access this object via actionscript like this (incorporating your code from the question):

var mysound:MySound = new MySound(); 
mysound.addEventListener(Event.COMPLETE, completeHandler); 
mysound.play();

note: if you want to further control the sound (stop, rew, etc), you'll need to create a SoundChannel object. (see documentation below)

Adobe Sound Object Documentation

like image 91
turkeyburger Avatar answered Oct 26 '22 13:10

turkeyburger


The top answer has managed to get some of the code incorrect.

Here is a more simplistic version.

var mysound:Sound = new (mySound); 
mysound.play();

}

Just change your internal sound file class name to mySound.

like image 23
Michael Scott Avatar answered Oct 26 '22 14:10

Michael Scott