How to Clear a Sound Channel in AS3

Save

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.

Things You'll Need

  • Adobe Flash Professional (CS3, CS4 or CS5 Versions)
  • Start your Adobe Flash software and open the Flash ActionScript 3 file that contains your sound program.

  • 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 myRequest: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();

    }

    }

  • 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.

  • 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 myRequest: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;

    }

    }

References

  • Photo Credit Jupiterimages/BananaStock/Getty Images
Promoted By Zergnet

Comments

Resources

Related Searches

Check It Out

Can You Take Advantage Of Student Loan Forgiveness?

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!