CS 548  Flash and ActionScript - ActionScript 3.0 Sound Control
Professor Lynne O’Hanlon
Sound and Movie Start, Stop, Resume, Volume Control

Part 1
Review Movie Clip and Button Symbols using ActionScript 3.0

  • Instance Names in Properties Inspector for Buttons and Movie Clips
    • _btn for button instances
    • _mc for movie clip instances
  • ActionScript 3.0 can only be added to Timeline in Key Frames, no longer to Buttons
    • Simple actions such as stop ( ); can be added to Timeline.
    • ActionScript 3.0 cannot ever be added to a Graphic Symbol
  • Events, Event Listener, Event Handler
    • Event – something happens, user clicks mouse, file finishes loading, key pressed
      • Classes defined in ActionScript define various events, such as MouseEvent
      • Within each Class are specific events, such as CLICK, DOUBLE_CLICK, COMPLETE
    • Event Listener – addEventListener to an Instance to Listen or Wait for a specific event to occur.
      • Specific class and event are defined in parameter list
      • Specific function to run (Event Handler) listed in parameter list

 

    • Event Handler – user-defined function written that runs when event occurs.
      • Event information passed into event variable in parameter list
      • Statements inside function define what to do to movie clips and sounds by addressing the Object’s Instance Name, a dot, and then the specific method to run.

Part 2
Lecture Material
Programming Notes:
Any line of code beginning with var creates a variable of the specified type:
            var position:Number;

Variables store information and the location of the variable in a computer is accessed using the variable name.  In this example a number will be stored in the variable position.

ActionScript uses Classes – such as Sound (to store sound files loaded into a Flash file at run time), SoundChannel (to control stop, play and resume), SoundTransform (to control volume).  The name of a class always begins with a capital letter (Sound, SoundControl, SoundTransform)

When we create a variable from a class, it is called an instance.  There are several ways to create an instance.  The first is to use var followed by the instance (variable) name, colon, the class name and then = sign, the word new (to create a new instance) followed by the class name and parentheses.
var sound:Sound = new Sound( );
This creates an instance (variable) called sound from the Sound Class.

            var soundControl:SoundControl = new SoundControl ( );
This creates an instance (variable) called soundControl from the SoundControl Class.

var volumeControl:SoundTransform = new SoundTransform ( );
This creates an instance (variable) called volumeControl from the SoundTransform Class.

The Sound class does not give us very much control over our sound beyond loading and
playing. To obtain more control we will use the SoundChannel class to stop, resume, and play a sound.
Create a new instance of the SoundChannel class: 
            var sChannel:SoundChannel;

Create a variable for the music:
            var music:Hype = new Hype ();
See later note (2 below) on Linkage properties to create this new class for sound files in the Library.

You would think that we could use sChannel.play( ) to play our sound, just as we use sChannel.stop( ) to stop it. But this is not the case. Instead, we still use the play( ) method of the sound object itself (sound) to play the sound, and we simply attach it to the SoundChannel variable.

In function playSound play the music.  The play function is attached to the music variable and stored in the sound channel variable.  Sometimes the play function is used without any arguments (values inside the parentheses).  Other times values are included inside the parentheses for use in the function.  When values are included, the first number in parentheses is the position (time in milliseconds) to begin playing and the second number in parentheses is the number of times to loop the song:
sChannel = music.play (0,100);

Sound Classes and Sound Objects

  • Add Sound to Library
    • File > Import > Import to Library
    • Test Sound by click Play and Stop Button in Library
  • Create New Class for Sound
    • Right Click Sound file in Library
    • Select Linkage
    • Check  “Export for ActionScript”
    • Give sound file a new Class Name (Tesh, Hype, etc).
    • Click OK
  • ActionScript 3.0 in Actions Panel
    • Create new object for sound class
      • var mySound:Tesh = new Tesh ( );
    • Create SoundChannel variable to control sound, such as play, stop
      • var soundControl:SoundChannel;
    • In play function call the play method
      • soundControl = mySound.play ( 0, 100);
      • 0 is how long to wait before beginning sound
      • 100 is maximum number of times to repeat
    • In stop function call the stop method
      • soundControl.stop ( );

play_btn.addEventListener(MouseEvent.CLICK, playMovie);     // Add Event Listener to play_btn
stop_btn.addEventListener(MouseEvent.CLICK, stopMovie);     // Add Event Listener to stop_btn

var soundTrack:Tesh = new Tesh();                // Tesh Class you created during Linkage step in Flash
var sndControl:SoundChannel;                        // Object you create to control play
var resumeTime:Number = 0;                          // Variable holds music position for resuming play
 
function playMovie (event:MouseEvent):void                  // Event Handler to play movie and music
{
                AnimalMovie_mc.play();                                              // Play movie when play_btn is clicked
                sndControl = soundTrack.play(resumeTime,10);        // resume sound at resumeTime position
               
}

function stopMovie (event:MouseEvent):void                  // Event Handler to stop movie and music
{
                AnimalMovie_mc.stop();                                                  // Stop movie when stop_btn is clicked
                resumeTime = sndControl.position;                                // Store location of where sound stops
                sndControl.stop();                                                             // Stop sound
}


img2img4


 

Part 3
Sound Demonstration
Sounds Using ActionScript 3.0

  • Animals and Music with Music On Off Buttons

img6

 

 

 

 

 

 

 


  • AnimalParkAnimalsMovie with Buttons and Music Both Stop (No Resume)

 

img8

 

 

 

 

 

 

 

 


  • AnimalParkAnimalsMovie with Buttons and Music With Resume

img10

 

 

 

 

 

 

 

 

 

 

 

 

  • Ball Bouncing With Bonk on Ball Bounce 3.0
    • ActionScript 3.0 added in Ball Movie Clip
    • img12

 

 

 

 

 

 

 

 


 

  • Volume Control 3.0
  • img14

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Additional Sound Information

In addition to the previous examples shown in class, the following additional information may be useful.

The following examples use URLRequest to load the sound file when the flash file plays (and does not embed the sound in the Flash file).  This makes the Flash file smaller but requires a bit of additional code to load the sound file.

1.  Load a sound file using URLRequest.
Create a variable to request to load a sound file that is in the same folder as the fla and swf files:
var soundReq:URLRequest = new URLRequest (“filename.ext”);

The URLRequest class is used to load a sound file.  The URLRequest instance or variable is soundReq.

Create a sound variable called sound.  The class is Sound (capital S) and the instance or variable of this class is sound (lower case s).
            var sound:Sound = new Sound( );

Write the instruction to load the sound:
            sound.load(soundReq);

The load function is called by the sound variable.  The statement includes the sound variable in ( ).

The sound variable (soundReq) just holds the sound file and cannot control any other sound functionality.  The only action we can apply using the sound class is play:
            sound.play( );

Note the . (dot) separating the instance or variable name and the function.
In some cases there are numbers in the parentheses after play and some cases nothing is included in the parentheses.

The complete code to load and play “hype.mp3” – a sound file that is in the same folder as the swf file:

var soundReq:URLRequest = new URLRequest(“hype.mp3”);
var sound:Sound = new Sound ( );

sound.load(soundReq);

sound.addEvent:Listener (Event.COMPLETE, onComplete);

function onComplete (event:Event):void
{
            sound.play ( );
}

While this example is a good start, the statement sound.play( ); does not give us any control of stopping the sound, resuming the sound from a stopped position, or increasing or decreasing the volume.

2.  Sound Playback – start and stop a sound file
To load a sound file (that is not embedded into a swf file)  use the URLRequest class and sound class.

To playback a sound, use the SoundChannel class.  This statement creates an instance (variable) called soundControl created from the class SoundControl.
            var soundControl:SoundControl = new SoundControl ( );

Add buttons for user interactivity: play_btn to play the sound and stop_btn to stop the sound. (play_btn and stop_btn are the names of the buttons in the Properties Inspector.)

To make the buttons react to user clicks, add event listeners to each button and call an appropriate function to play or stop the sound:
play_btn.addEventListener (MouseEvent.CLICK, playSound);
stop_btn.addEventListener (MouseEvent.CLICK, stopSound);

Write the function (event handler)  for playSound and stopSound:
function playSound (event:MouseEvent) : void
{
            soundControl = sound.play ( );
}
function stopSound (event:MouseEvent) : void
{
            soundControl.stop ( );
}

The full code to load sound, start and stop the sound with play and stop buttons:
var soundReq:URLRequest = new URLRequest(“hype.mp3”);
var sound:Sound = new Sound ( );
var soundControl:SoundControl = new SoundControl ( );

play_btn.addEventListener (MouseEvent.CLICK, playSound);
stop_btn.addEventListener (MouseEvent.CLICK, stopSound);

sound.load(soundReq);

function playSound (event:MouseEvent) : void
{
            soundControl = sound.play ( );
}

function stopSound (event:MouseEvent) : void
{
            soundControl.stop ( );
}

3.  Volume Control
To control sound volume you need to use the SoundTransform Class.
Create a variable of the SoundTransform class:
var volumeControl:SoundTransform = new SoundTransform ( );

Create a button to increase the volume (up_btn) and a button to decrease the volume (down_btn)

Add an event listener to the up_btn and add an event listener to the down_btn
up_btn.addEventListener (MouseEvent:CLICK, upVolume);
down_btn.addEventListener (MouseEvent:CLICK, downVolume);

Write an event handler to increase and decrease sound volume.

function upVolume (event:MouseEvent) : void
{
            volumeControl.volume += .2;                                               
            soundControl.SoundTransform = volumeControl;  
}

function downVolume (event:MouseEvent) : void
{
            volumeControl.volume -= .2;                                    
            soundControl.SoundTransform = volumeControl;  
}

The complete code to load sound, start and stop sound, resume where sound stopped previously and increase and decrease volume:

var soundReq:URLRequest = new URLRequest(“hype.mp3”);
var sound:Sound = new Sound ( );
var soundControl:SoundControl = new SoundControl ( );
var volumeControl:SoundTransform = new SoundTransform ( );
var resumeTime:Number = 0;

play_btn.addEventListener (MouseEvent.CLICK, playSound);
stop_btn.addEventListener (MouseEvent.CLICK, stopSound);
up_btn.addEventListener (MouseEvent:CLICK, upVolume);
down_btn.addEventListener (MouseEvent:CLICK, downVolume);

sound.load(soundReq);

function playSound (event:MouseEvent) : void
{
            soundControl = sound.play (resumeTime );
}

function stopSound (event:MouseEvent) : void
{
            soundControl.stop ( );
            resumeTime = soundControl.position;
}

function upVolume (event:MouseEvent) : void
{
            volumeControl.volume += .2;                                               
            soundControl.SoundTransform = volumeControl;  
}

function downVolume (event:MouseEvent) : void
{
            volumeControl.volume -= .2;                                    
            soundControl.SoundTransform = volumeControl;  
}