Bits & Bytes

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 {

    import flash.media.SoundChannel;
    import flash.events.Event;
	
    public class CMyClass {
		
        var mqMusic:XoaxTheme;
        var mqSoundChannel:SoundChannel;

        public function CMyClass() {
            mqMusic = new XoaxTheme();
        }
		
        public function StartMusic(e:Event):void {
            mqSoundChannel = mqMusic.play();
            mqSoundChannel.addEventListener(Event.SOUND_COMPLETE,
                StartMusic);
        }
    }
}

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

var qMyClass:CMyClass = new CMyClass();
qMyClass.StartMusic(null);

The new code file needs to be saved. So, we select File->Save As… from the menubar and save the file as “CMyClass.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.

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

By: Michael Hall

One Response to “Looping Music or Sounds in Actionscript 3.0”

  1. Saori says:

    Thanks for spreading the words your the holy grail of Flash!!!It seems this couersal3.swf cannot be externally loaded to other SWF without error.For days I’m trying to debug the error when calling this couersal3.swf through loadMovie from my new .fla project (filename: myFlash.fla) with a dimensions of 1012 x 578 pixels. This couersal shows-up when it is being loaded but it doesn’t inherit it’s own timeline or root thus it is taking the dimension of the myFlash.fla which is (1012 578). And also because of this the position of the couersal is outside the boundaries of the movieClip holder.I tried to fix the conflict by placing this code > this._lockroot = true; to the main timeline of couersal.swf but it seems it doesn’t work because the couersal uses > var home:MovieClip = this; to refer to the main timeline. So this is already assigned to home that refers to the timeline which is causing conflict to the _lockroot. I also tried putting > myHolder._lockroot = true; to the main timeline of myFlash.fla but same, it doesn’t solve the problem.Mr. Lee I think you should look at this matter please show us some light again .How to restore inheritance of couersal’s own root or timeline when it is being loaded externally from other SWF movie?TNX

Leave a Reply

*