Bits & Bytes

Posts Tagged ‘3’

Looping Music or Sounds in Actionscript 3.0

For the second example, we demonstrate how to loop a music file using a separate class file. We start with the sound file “XoaxTheme.mp3” in our project and its associated class, XoaxTheme, that we had from our previous post.

To begin, we add an ActionScript code file to the project by selecting File->New from the menubar. This opens the “New Document” dialog shown below, where we left-click “ActionScript File” to select it and left-click the “OK” button to create a new code file.

Next, we paste this code into the new file:

package {

    public class CMyClass {
        var mqMusic:XoaxTheme;
        var mqSoundChannel:SoundChannel;

        public function CMyClass() {
            mqMusic = new XoaxTheme();
        public function StartMusic(e:Event):void {
            mqSoundChannel =;

and then change the code in the main code file to this:

var qMyClass:CMyClass = new CMyClass();

The new code file needs to be saved. So, we select File->Save As… from the menubar and save the file as “” in the project folder. Finally, we can compile and execute the code, and it will play the music repeatedly.

Let’s review the code above. In our class file, we use an unnamed package and import the SoundChannel and Event files. Inside the class, we have two members: mqMusic and mqSoundChannel, which are used to control the music. The class XoaxTheme is the sound class that we created in the library. We instantiate this class in the constructor.

The function StartMusic() does all of the real work in the program. When we call play(), the music starts playing and a SoundChannel object is created and returned. The SoundChannel controls the sound as it plays. We register a SOUND_COMPLETE event on the SoundChannel to call the function StartMusic() recursively when the music finishes playing. In this manner, we create the loop for repeatedly playing the music.

In the main program, we create an instance of a CMyClass object and then call StartMusic() on it to begin the music loop.

Playing Music or Sounds in Actionscript 3.0

For our first example, we demonstrate how to add a simple sound file to a newly-created project and play it. We start by adding the sound file to the project.

Go to the menubar and select
File->Import->Import to Library…

Then select the sound file that we want to play, something like an .mp3 or .wav file, and left-click the “Open” button. Then right-click the file under the “Library” tab to open the context menu and left-click “Properties…” in the context menu. This opens the “Sound Properties” dialog.

At this point, you might need to left-click the “Advanced” button to open the advanced options inside the “Sound Properties” dialog. In the “Linkage” area, left-click the check box next to “Export for Actionscript.” Then change the name next to the “Class:” box to something appropriate for the class name of your sound. The simplest idea for the class name is to just remove the file extension (.mp3 or .wav, for example). This is what it looks like for my example:

Finally, left-click the “OK” button to create the class. Now, “Export: XoaxTheme” is under “Linkage” in the “Library” tab next to the file name “XoaxTheme.mp3”.

We finish the program, by adding these lines in the “Actions – Frame” tab, where our main program code goes:

var qMySound:Sound = new XoaxTheme();;

Now, we can compile and execute our code from the menubar by selecting
Debug->Debug Movie

This plays the sound file that we added and finishes our first example. This simple example only uses two lines of code. In our next example, we demonstrate how to loop this music with the code in a separate class file.


© 2007–2022 LLC. All rights reserved.