Create two buttons that are sort of similar and represent sound on and off. For example you can use play/stop or speaker with sound waves/speaker with a cross buttons.
Import your sound file into your movie: Press (Ctrl+R) to get the Import screen, now browse to locate your sound file.
Open your Library panel (Ctrl+L), locate and right click on your sound file. From the list select Linkage... the linkage properties form will pop-up. In the identifier text box enter mix1 and tick the "Export for Actionscript" and "Export in first frame" options under Linkage.
What this Does: You are giving the sound file in the library an identifier without it actually being used in the movie timeline. This enables it to be called and used by ActionScript code.
In your movie timeline, create 3 consecutive key frames. Place your Play button in the second frame and label it as play and your Stop button in the third frame and label it as stop (The frame label is specified in the properties window). They should both be in the same position for them to look like a switch
Basic Logic: We are utilizing the inbuilt Sound Class in Flash MX to dynamically load the sound file and the on/off buttons to play and stop it.
Click the first key frame and copy-paste the following actionscript into the actions panel.
my_sound = new Sound();
my_sound.attachSound("mix1");
play();
What this Does: You are creating an instance of the Sound Class whose source is mix1 (the identifier in the library for your sound file).
Click the 2nd and 3rd frames and give the stop() action.
Select the play button in the 2nd frame and copy-paste the following actionscript code into the actions panel.
on (release) {
_root.my_sound.start(0,1000);
_root.gotoAndStop("stop");
}
What this Does: On clicking your play button, you are telling your sound instance to start playing and the timeline to go and display the stop button in the "stop" frame. The optional parameters (0,1000) for the start function are given for looping of the sound or music file. This specifies that the sound file starts playing at 0 milliseconds and is looped 1000 times (any number value depending on the duration of the sound file and how long you want it to loop).
Select the stop button in the 3rd frame and copy-paste the following actionscript code into the actions panel.
on (release) {
_root.my_sound.stop();
_root.gotoAndStop("play");
}
What this Does: On clicking your stop button, You are telling your sound instance to stop playing and the timeline to go and display the start button in the "play" frame.
That's it your sound on/off button is ready! Its that simple.
Cool Tip: You can use these buttons even within a movie clip as the sound instance is initiated in the main timeline.
To do this just do the following:
Click the first frame and in the actions panel place the following actionscript code:
my_sound.start(0,1000);
gotoAndStop("stop");
in place of the last line: play;
Your ActionScript for the first frame will look as follows.
my_sound = new Sound();
my_sound.attachSound("mix1");
my_sound.start(0,1000);
gotoAndStop("stop");
What this Does: You are telling your sound instance to play as soon as it is initiated and the timeline to go and display the stop button in the "stop" frame.
by cashleycool:Happy: