70 likes | 226 Views
Sound File. Step 1 : Import Sound File. Import your sound in the library selecting File > Import > Import to library . In the library right click on your sound file and select properties. Select the ‘Export to Actionscript ’ box and give the Class name ‘ MySound ’ and click ok. Step 2.
E N D
Step 1 : Import Sound File • Import your sound in the library selecting File > Import > Import to library. • In the library right click on your sound file and select properties. • Select the ‘Export to Actionscript’ box and give the Class name ‘MySound’ and click ok.
Step 2 Select Insert > New Symbol and choose movie clip for the type and click ok. Frame 1 Frame 2 Background
Now the code – you just need to copy and paste this to the main timeline //This stops the toggle buttons from playing the second frame. toggle_btn.stop(); //Variable to detect whether the number of times the toggle button has been clicked. varclickOnce:uint=0; //Creates a new instance of the imported sound. varmyLionSound:Sound= new LionSound(); //Create a new instance of the SoundChannel varaChannel:SoundChannel = new SoundChannel(); //This adds the mouse click event to the toggle button. toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay); //If the toggle button has been clicked once then the sound plays //with an image of a stop symbol. If the toggle button is clicked //again then sound stops and the play symbol is displayed. function togglePlay(event:MouseEvent):void { clickOnce++; if (clickOnce==1) { aChannel=myLionSound.play(); toggle_btn.gotoAndStop(2); } if (clickOnce==2) { SoundMixer.stopAll(); toggle_btn.gotoAndStop(1); clickOnce=0; } } Sound – should be the called the same as the sound file in your library
Extra Code • When the sound has finished playing you can also return back to the play symbol with the following. • The code uses the sound complete event to detect when the sound file has finished playing: aChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleted); function soundCompleted(event:Event):void { toggle_btn.gotoAndStop(1); }
Controlling Sound • To stop sound playing: SoundMixer.stopAll();
Controlling Multiple sounds //This stops the toggle buttons from playing the second frame. main_menu.mainmenunar.stop(); //Variable to detect whether the number of times the toggle button has been clicked. varclickOnce:uint=0; //Creates a new instance of the imported sound. varmainmenunarVar:Sound= new mainmenunarsnd(); varambience:Sound= new cityamb(); //Create a new instance of the SoundChannel varaChannel:SoundChannel = new SoundChannel(); var aChannel2:SoundChannel = new SoundChannel(); //Play ambience sound aChannel2 = ambience.play(); //This adds the mouse click event to the toggle button. main_menu.mainmenunar.addEventListener(MouseEvent.CLICK, togglePlay); //If the toggle button has been clicked once then the sound plays //with an image of a stop symbol. If the toggle button is clicked //again then sound stops and the play symbol is displayed. function togglePlay(event:MouseEvent):void { clickOnce++; if (clickOnce==1){ //SoundMixer.stopAll(); aChannel2.stop(); aChannel=mainmenunarVar.play(); main_menu.mainmenunar.gotoAndStop(2); } if (clickOnce==2) { SoundMixer.stopAll(); main_menu.mainmenunar.gotoAndStop(1); clickOnce=0 aChannel2 = ambience.play(); } }