Class Variables in ActionScript3

Get Adobe Flash player

In order to play a sound, you either need to have the sound in the library, or play the sound from a URL.
In this example we will play a sound from a URL..

  1. Start a new flash movie.
  2. From the common libraries, buttons, drag a button to the stage and name the instance on the stage btnWin.
  3. Write the code to play the sound directly from the URL

Write the code as shown below:

btnWin.addEventListener(MouseEvent.CLICK, winner);

function winner(e:MouseEvent):void {
  var sound:Sound=new Sound();
  var req:URLRequest = new URLRequest("http://www.zebra0.com/resources/sounds/win.mp3");
  sound.load(req);
  sound.play();
} //winner

Play a sound from the library

Download the finished movie

NEXT: Start and Stop a Sound with a Channel