Adding the ActionScript

First, we'll add the code that controls the playback of the sounds we imported.

  • Select the first frame of the Actions layer and add the following code within the Actions Panel:
  • Example 5.4. miniplayer.fla Actions : 1 (excerpt)

    function randomBetween (a, b)
    {
            return Math.min (a, b) + random (Math.abs (a - b) + 1);
    }
    PlayClip.onPress = function ()
    {
            stopAllSounds ();
            _root.orchestra = new Sound ();
            _root.orchestra.attachSound ("choir" + randomBetween (1, 5));
            _root.orchestra.start ();
    };

    As the music player plays clips at random, our familiar randomBetween function comes in handy.

    As the heart of this example, the onPress event handler of the PlayClip button definitely merits further study. The first thing it does is stop any sound that may already be playing, so that multiple sound clips aren't played at the same time if the PlayClip button is clicked repeatedly. The built-in stopAllSounds function handles this:

     

    stopAllSounds ();

    Next, we create a new Sound object and store it in a variable in the main movie (_root) called orchestra. We then use attachSound to load one of the five sounds from the library start it playing:

     

    _root.orchestra = new Sound ();
    _root.orchestra.attachSound ("choir" + randomBetween (1, 5));
    _root.orchestra.start ();

  • Save and preview your work.
  • Press the Play button and a randomly selected clip will begin to play; press it again and another random clip will play. It's all functioning as expected.

    Now, we'll add the Stop button's control code. This is much the same as the code we used with the Play button to remove the movie clips and stop all sound on the stage:

  • Add the following code beneath what you already have:
  • Example 5.5. miniplayer.fla Actions : 1 (excerpt)

    StopClip.onPress = function ()
    {
            stopAllSounds ();
            _root.orchestra = null;
    };

     

    We use stopAllSounds to stop any playing sounds, and throw away the Sound object we'd stored in the orchestra variable. This frees up the memory the object was using.

  • Save and preview your work.
  • When you press the Play button, the sound plays; when you hit the Stop button, it stops. Let's add the volume control buttons and finish this example.

  • Insert the following code beneath what you already have:
  • Example 5.6. miniplayer.fla Actions : 1 (excerpt)

    VolumeUp.onPress = function ()
    {
            if (_root.orchestra.getVolume () < 100)
            {
                    _root.orchestra.setVolume (_root.orchestra.getVolume () + 10);
            }
    };
    VolumeDown.onPress = function ()
    {
            if (_root.orchestra.getVolume () > 0)
            {
                    _root.orchestra.setVolume (_root.orchestra.getVolume () - 10);
            }
    };

    When the volume of a sound clip is less than 100, we increase it in increments of ten each time the VolumeUp button is pressed. Conversely, when its volume is greater than zero, it's decreased in steps of ten each time the VolumeDown button is pressed.

  • Save and preview your work.
  • You can select a random clip by pressing the Play button, stop the dynamically loaded clip with the Stop button, and increase or decrease the volume of the clip using the volume controls. You may want to experiment with sound clips of your own, as those distributed in the code archive are quite short.