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;
}
}