Bits & Bytes

Loading and Playing a Sound File in Actionscript 3.0

This program loads an mp3 file and plays it. The entire program is given below and the compiled SWF is shown above. To hear the file play, left-click the window above.

The code begins with three lines that tell the user to click the screen. Then we create a String for the URL, which could also be a local file. Next, we set our function as the callback for mouse clicks. Inside the function OnClick(), we create a Sound object, load the sound file, and then play it.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";

const ksXoaXDotNet:String = "";

// Play the mp3 when the screen is clicked
stage.addEventListener(MouseEvent.CLICK, OnClick);

function OnClick(e:MouseEvent): void {
	var qSound:Sound = new Sound();
	qSound.load(new URLRequest(ksXoaXDotNet));;

Tags: , , , , , , , , , , , ,

By: Michael Hall

One Response to “Loading and Playing a Sound File in Actionscript 3.0”

  1. Ali says:

    I tried this code and I keep getting SecurityError: Error #2000: No active security context.

Leave a Reply