How to Clear a Sound Channel in AS3

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Clear a Sound Channel in AS3
Edit your audio recordings with AS3 Sound and SoundChannel classes (Jupiterimages/BananaStock/Getty Images)

SoundChannels, AS3 objects that are needed to stop a sound from playing and find the position of the Flash sound playhead, are used to construct Flash music players, create audio editing tools, build Flash Internet radio stations and add dynamic sound effects to video games. Clearing a SoundChannel removes the actual song file from the SoundChannel, ensuring that the sound stops playing and cannot be inadvertently replayed. Clearing an AS3 SoundChannel is a simple process that requires the use of the null value, which clears an AS3 variable of any values assigned to it.

Skill level:
Easy

Other People Are Reading

Things you need

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

Show MoreHide

Instructions

  1. 1

    Start your Adobe Flash software and open the Flash ActionScript 3 file that contains your sound program.

  2. 2

    Click "Actions" from the "Window" menu option on the Flash main menu bar to view the AS3 sound code. Use the code below as your sample AS3 sound code in the Actions editor for this example.

    var nobody:Sound = new Sound();

    var my Request:URLRequest=new URLRequest("nobody_knows.mp3")

    nobody.load(myRequest);

    var sirenchannelone: SoundChannel = new SoundChannel();

    stage.addEventListener(MouseEvent.CLICK, bluesplay)

    Labelstop.addEventListener(MouseEvent.CLICK, violetplay)

    function bluesplay (songevent:MouseEvent): void {

    sirenchannelone = nobody.play();

    }

    function violetplay (songevent:MouseEvent): void {

    if (sirenchannelone){

    sirenchannelone.stop();

    }

    }

  3. 3

    Observe that the code declares a Sound object called "nobody," loads an mp3 sound file called "nobody_knows.mp3" into this sound object and plays the song when the mouse button is clicked. Also note that the "Stop" method of the SoundChannel object is used to stop playing the song when a button (on the stage) with an instance name called "LabelStop" is clicked.

  4. 4

    Assign a "null" value to the sirenonechannel object to "clear" the reference to the sound file "nobody_knows.mp3" that is in the "sirenonechannel" object. Insert this code directly after the "sirenonechannel.stop();" statement within an AS3 "if" statement as shown below. Specify the "if" statement so that it will detect if there is a variable (other than "null") within the "sirenonechannel" object (as illustrated below).

    var nobody:Sound = new Sound();

    var my Request:URLRequest=new URLRequest("nobody_knows.mp3")

    nobody.load(myRequest);

    var sirenchannelone: SoundChannel = new SoundChannel();

    stage.addEventListener(MouseEvent.CLICK, bluesplay)

    Labelstop.addEventListener(MouseEvent.CLICK, violetplay)

    function bluesplay (songevent:MouseEvent): void {

    sirenchannelone = nobody.play();

    }

    function violetplay (songevent:MouseEvent): void {

    if (sirenchannelone){

    sirenchannelone.stop();

    sirenchannelone = null;

    }

    }

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.