Chapter 31 Example 2

by actionscriptbible
♥0 | Line 27 | Modified 2010-02-05 05:09:59 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ms4i
 */

package {
  import com.actionscriptbible.Example;
  import flash.events.MouseEvent;
  import flash.media.*;
  import flash.net.URLRequest;

  public class ch31ex2 extends Example {
    protected var activeSound:SoundChannel;
    protected var winterSong:Sound;
    public function ch31ex2() {
      //The Four Seasons: Winter [performance & recording in the public domain]
      //composed by Antonio Vivaldi, performed by the US Air Force Band
      winterSong = new Sound();
      winterSong.load(
        new URLRequest("http://actionscriptbible.com/files/winter.mp3"),
        new SoundLoaderContext(5000, true)
      );
      stage.addEventListener(MouseEvent.CLICK, onClick);
      activeSound = winterSong.play();
      trace("Playing song...");
    }
    protected function onClick(event:MouseEvent):void {
      if (activeSound) {
        activeSound.stop(); //it's that easy
        activeSound = null;
        trace("Stopped!");
      }
    }
  }
}