Looping sounds in ActionScript 3

ActionScript 3 has done quite a few things differently to ActionScript 2. I was doing a lot of programming in C# before I got a chance to play around with it and it looked quite familiar – maybe the whole Object Oriented Programming things is catching on.

Anyway – tried to loop a sound clip in Flash and it was a lot more painful than I expected so I made a note in my work diary that looked like this –

25 February 2009

To loop a sound clip in Flash using ActionScript 3 refer to –
http://doogog.com/music-looping-in-as3.html

The code I developed also has a method to stop the music using a button is –

   1:  //Stop at this frame
   2:  stop(); 
   3:   
   4:  //Create a new sound object
   5:  var music:Sound = new Sound()
   6:   
   7:  //Set location of sound file – if it is external – using a URLRequest object
   8:  var req:URLRequest = new URLRequest("audio/bongos.mp3");
   9:   
  10:  //Create a new SoundChannel object
  11:  var channel:SoundChannel; 
  12:   
  13:  //load music file – via url request object – into channel object
  14:  music.load(req);
  15:  playMusic(); 
  16:   
  17:  //Boolean checks if the sound is playing
  18:  var musicIsOn:Boolean = true;
  19:   
  20:  //An event listener for a button that stops the music
  21:  btnMusic.addEventListener(MouseEvent.CLICK, stopMusic); 
  22:   
  23:  function stopMusic(evt:Event):void {
  24:      if(musicIsOn)
  25:      {
  26:          trace("stop");
  27:          channel.stop();
  28:          musicIsOn = false;
  29:      } else {
  30:          playMusic();
  31:          musicIsOn = true;
  32:      }
  33:  } 
  34:   
  35:  function playMusic():void{
  36:      trace("play");
  37:   
  38:  //play music through the SoundChannel
  39:      channel = music.play();
  40:   
  41:  //add an event listener that reacts when the sound being played 
  42:  //is completed and repeats the sound
  43:      channel.addEventListener(Event.SOUND_COMPLETE, repeat);
  44:  } 
  45:   
  46:  function repeat(evt:Event):void {
  47:      if(channel != null)
  48:      {
  49:   
  50:  //without removing the event listener the sound clip will play only twice
  51:  //I am not sure why but it feels like the event listener gets disposed of
  52:  //once used by default.
  53:          channel.removeEventListener(Event.SOUND_COMPLETE, repeat);
  54:          playMusic();
  55:      }
  56:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

I hope you found that useful.

Advertisements

12 thoughts on “Looping sounds in ActionScript 3”

  1. To repeat the sound over and over forever:

    addEventListener(Event.SOUND_COMPLETE, repeat, false, 0, true);
    function repeat(e:Event):void {
    sc = music.play(0, int.MAX_VALUE);
    }

    In some case ( to avoid “Cannot access a property or method of a null object reference”) you need to add this code inside of if:

    if (channel) {
    //channel.addEventListener….etc.
    }

    Have a nice day,

  2. i’m a designer not a coder. would someone please help me out with where to put Everin’s edits about repeating the soudn forever? I can’t figure out where to put them in the original code. thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s