Swami Charan's Blog My personal blog…

6Sep/092

Working with Sound in AS 3.0

In this post, we are going to look at handling, playing and dealing with Sound in Adobe Flash AS3.

The simplest way to play any local mp3 sound is that you need to make a URLRequest to the mp3 file and pass it to Sound object.

var req:URLRequest = new URLRequest("sample.mp3");
var mySound:Sound = new Sound(req);
mySound.play();

Checking the loading of the Sound

Sound class can dispatch events like ProgressEvent.PROGRESS and Event.COMPLETE to check the loading process of a Sound File. Using ProgressEvent.PROGRESS event, we can keep track of how much of the Sound Data is being loaded and how much sound has been played.

 

import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.net.URLRequest;

var req:URLRequest = new URLRequest("sample.mp3"); 

var snd:Sound = new Sound();
snd.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
snd.addEventListener(Event.COMPLETE, onLoadComplete); 

snd.load(req);

function onLoadProgress(event:ProgressEvent):void
{
    var pcntLoaded:uint = Math.round(100 * (event.bytesLoaded / event.bytesTotal));
    trace("The sound is " + pcntLoaded + "% loaded.");
} 

function onLoadComplete(event:Event):void
{
    var localSound:Sound = event.target as Sound;
    localSound.play();
}

 

Event.COMPLETE  event will be dispatched when the Sound is completely loaded.

In the example above, the variable pcntLoaded will get the amount of Sound loaded. Once the sound is loaded completely, localSnd.play() will start playing the Sound File.

Embedding Sound

There is a provision in Flash of embedding Sound into Flash Authoring Tool as a Symbol to be stored in Library. Only thing to be noted while embedding sound is that the size of the SWF file is going to increase by the size of the Sound File embedded. The larger the sound file embedded, the larger will be the size of the resulting SWF.

Here are the steps to be followed to embed a sound into Flash Library:

  1. Goto File->Import->Import to Library, Select the sound File and import it to Library.
  2. Right-click the selected Sound File in Library and Select Properties.
  3. Then Check the Export to ActionScript checkbox.
  4. Enter a name in 'class' field to refer it as embedded sound in actionscript. Say, if the file name is 'sample.mp3', enter the class name as 'sample'. The base class will be showing 'flash.media.Sound'.
  5. Click OK and click OK on the dialog which appears next. Now a new clas 'Sample' is being inherited from 'flash.media.Sound'. We can use this class in actionscript to refer to this embedded sound as follows:
var mySnd:Sample = new Sample();
var channel:SoundChannel = mySnd.play();

 Dealing with Streaming Sound

While trying to load a sound file which is located some remote server, the sound is played while its being loaded. Till now we were dealing with Local Sound Files, which dont have to stream it while playing.

When trying to play any Sound or video file from remote server, that file will be buffered while its being played. There is a property SoundMixer.bufferTime represents the time in milliseconds of sound data to be gathered before Flash starts playing it. Effectively, flash will wait until SoundMixer.bufferTime milliseconds of sound data is buffered before starts playing it. The default value of SoundMixer.bufferTime is 1000 ms.

In order to change this default value of buffertime, we need to create a new instande if SoundLoaderContext and set the desired buffertime and send it to Sound.load()

import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest; 

var snd:Sound = new Sound();
var req:URLRequest = new URLRequest("sample.mp3");
var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
snd.load(req, context);
snd.play();

In the above example, we are setting the bufferTime to 5000ms i.e, 5 sec.

Pause/Resume

Flash actionscript does not have any function to pause and resume the sound. We have only play() and stop() in actionscript. However, we can play a sound from any point in the whole sound duration. We can implement Pause/Resume functionality by playing the sound from the instant where it has been paused.

We have SoundChannel.position method which represents the current moment of sound play. We can store the moment when the sound is paused and we can start playing from this moment when resumed using Sound.play(position).

 

var req:URLRequest = new URLRequest("sample.mp3");
var snd:Sound = new Sound(req);
var channel:SoundChannel = snd.play();

var pausePos:int = channel.position;
channel.stop();

Here we got have recorded the position at which User wants to pause. To resume playing from this instant, just pass this position to Sound.play() method.

channel = snd.play(pausePosition);

 

Stopping Streaming Sounds

To stop streaming a sound file from a remote server, we have to call stop() method on SoundChannel i.e, SoundChannel.stop().

The problem with this is that, it wont stop streaming permanently. It will stop streaming at that instant, but it will playback from the start in the next frame.

To stop this, we need to stop streaming as well as loading Sound i.e, Sound.stop().

 

Hope this post will make it clear in basic sound handling in Flash AS3.

Comments (2) Trackbacks (0)
  1. SoundChannel doesn’t have close(), it only has stop(). Still, your tutorial was very helpful in helping my understanding of streaming sounds in Flash. Thank you.

  2. Thanks Cardin for pointing it out. That was a typo…


Leave a comment

(required)

No trackbacks yet.