Flash Tutorials

Flash Sound On/Off Button

On-off button in Flash Actionscripting for Music files

Flash Sound On/Off Button

On-off button in Flash Actionscripting for Sound/Music files

This Flash tutorial teaches how to play and stop sound/music files using simple actionscript.

You Are Here ::: Home >> Free Tutorials >> Flash Tutorials >> Sound On Off Button

 

Sound on/off Demo

Introduction

While working in Flash with sound/music files you will find it really necessary to use a sound on/off button as an option. We have endeavored to make the action scripting used in this tutorial as basic & minimal as possible. Happy Learning!

Please take a look at the demo above. This demo is a simple play/stop button for you to control the sound files in your Flash project. Usually you will need to have the music playing and have a stop button displayed first. Since it is easier to understand how a play and then stop button works we have used this example first. Once this is done we will also teach you how to start with the sound playing and the stop button displayed.

Preparing buttons, importing your sound file and setting the timeline

  • 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 (see demo).

Making your sound on/off buttons functional using ActionScript

Basic Logic: We are utilizing the inbuilt Sound Class in Flash 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.

Want to start with the sound playing?

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.

Well Done! You now know how to play and stop sound/music files using simple ActionScripting.

Software Required

Compatible Versions of Flash for this Tutorial

Recommended Version: Adobe Flash Professional CS5Recommended Version: Adobe Flash CS4 Professional
Flash CS5 Professional | Flash CS4 Professional
Note: For uses of lower versions of Flash some visual interface images would differ.
Recommended Version: Adobe Flash CS5 Professional
Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!