← back to overview

HowTo: Play MP3 with ActionScript 3.0

Playing MP3 from a http source (fake streaming) is quite easy in AS3. It's all done by the flash.media.Sound class.

var mySound:Sound = new Sound;
mySound.load(new URLRequest(myFileURL));
mySound.play();

One possible pitfall (It took me a few minutes to figure this out): The stop()-method and position-property are not part of Sound, but rather SoundChannel.

var mySoundChannel:SoundChannel;
mySoundChannel = mySound.play();

Stopping the playback:

private function stopPlayback:void(){
  mySoundChannel.stop();
  try{
    mySound.close();	
  } catch(error:IOError) { ... }			
}

The volume can be set by assigning a SoundTransform object:

soundtransform = new SoundTransform(0.75, 0);
mySoundChannel.soundTransform = soundtransform;

Note: The Sound class can't load a second file after one has been played. To play another file instantiate a new Sound object.