How to Stop Sound in AS3

You can create a complete orchestra of sounds with hundreds of musical instruments with Flash animation software. You can use prerecorded musical instrument sounds from MP3 files or even mathematically generate musical instrument sounds to create your movie soundtrack. For such projects, and even much simpler ones, you need to be able to stop sounds. With AS3 (ActionScript 3, the Flash programming language) you can stop specific sounds or stop all sounds. Stopping all sounds takes just one line of code; making the interface play and stop sounds is a little more work.

Things You'll Need

  • Adobe Flash Professional (Versions CS3, CS4 or CS5)
Show More

Instructions

    • 1

      Start the Flash program. Click "File," "New" on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the "New Document" dialog box. Click "OK."

    • 2

      Click the "Library" option from the "Window" menu.

    • 3

      Click "Sounds" in the "Common Libraries" menu within the "Window" menu.

    • 4

      Click one of the "sirens" sounds. Drag it to the Library panel that you already opened.

    • 5

      Right-click on your "sirens" sound file. Click "Properties" from the context menu. Check "Export for ActionScript." Replace the text in the "Class" text box with a new class named "Siren." Click "OK." Click "OK" again in the next "Class" dialog box.

    • 6

      Click "Properties" from the "Window" menu to display the Property Inspector. Click "Components" from the "Window" menu to display the Component Library.

    • 7

      Click and drag the "button" component from the Components Library to the Flash stage. Type the instance name "redsongs" into the instance name text box in the Properties Inspector. Click and drag another "button" component from the Components Library to the Flash stage. Type the instance name "Labelstop" into the instance name text box in the Properties Inspector.

    • 8

      Select "Actions" from the "Window" menu on the main Flash menu bar. Position your cursor on the first line of the Actions editor. Click and type in the following code to declare a Siren sound object and a soundChannel to process the sound through.

      var sirensong:Siren = new Siren();

      var sirenchannelone: SoundChannel = new SoundChannel();

    • 9

      Type the following code, starting at the next line in the Actions editor, to instruct Flash to play the siren sound when the redsongs button is clicked and to stop playing the siren when the Labelstop button is clicked.

      redsongs.addEventListener(MouseEvent.CLICK, redplay);

      Labelstop.addEventListener(MouseEvent.CLICK, violetplay);

      function redplay (songevent:MouseEvent): void {

      sirenchannelone = sirensong.play();

      };

      function violetplay (songevent:MouseEvent): void {

      if (sirenchannelone){

      SoundMixer.stopAll();

      } ;

      };

    • 10

      Copy and paste the following code into the Actions editor if you want to ensure that your sound creation and stopping program runs correctly.

      var sirensong:Siren = new Siren();

      var sirenchannelone: SoundChannel = new SoundChannel();

      redsongs.addEventListener(MouseEvent.CLICK, redplay);

      Labelstop.addEventListener(MouseEvent.CLICK, violetplay);

      function redplay (songevent:MouseEvent): void {

      sirenchannelone = sirensong.play();

      };

      function violetplay (songevent:MouseEvent): void {

      if (sirenchannelone){

      SoundMixer.stopAll();

      } ;

      };

    • 11

      Click "TestMovie" in the "Control" menu on the main Flash menu bar to start the siren sound movie. Click the redsongs button to play the sound siren and click the Labelstop button to stop the sound.

Learnify Hub © www.0685.com All Rights Reserved