How to Adjust the Audio Portion of Flash Video

Flash ActionScript 2.0

If you’re not using the FLVPlayback Component, or one of the older Media Components, then the audio portion of video files may have you scratching your head.  The Components have their own volume sliders, which makes volume control a snap, but what about panning (left to right fading), or what if you’re not using Components for video?  In ActionScript 2.0, video sound is a bit … well, it’s a bit odd, but once you understand it, audio control isn’t hard. 

An answer, short and sweet

In Understanding the Sound Constructor, I went into the usefulness of associating Sound instances with movie clips.  To control the audio portion of FLVs, it’s just a bit more of the same.  First, we’ll start with the basic “recipe” for bringing video into a Video object:

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
videoPlayer.attachVideo(ns);
ns.play("myExternalVideo.flv");

This is the block of code used in How to Load External Video (FLV) and brings the specified FLV file into a Video object with the instance name videoPlayer.  Invoking NetStream methods on the ns instance (such as NetStream.pause()) controls the visual portion, but what’s missing is sound.  Here’s how to do it.  After the opening block of code, type the following:

this.createEmptyMovieClip("videoAudioContainer", this.getNextHighestDepth());
videoAudioContainer.attachAudio(ns);

var videoVolume:Sound = new Sound(videoAudioContainer);
videoVolume.setVolume(50);

How it works

Four lines; two things going on.

First, the MovieClip.createEmptyMovieClip() method is invoked on the global this property, which refers to the main timeline if you’re typing this code into a main timeline keyframe.  This dynamically creates a new MovieClip instance at the next highest available depth, with the instance name videoAudioContainer.  The instance name doesn’t especially matter; just keep in mind, this is a movie clip “container” made just for your video’s audio portion.  If you wanted to, you could skip this step and put your own empty movie clip on the Stage.  Just make sure it has an instance name.  On this new instance, the MovieClip.attachAudio() method associates the NetStream instance ns with this movie clip.

Second, a new Sound instance, videoVolume, is instantiated and associated — this is the important part! — is associated with the videoAudioContainer clip that is associated with the NetStream instance.  All in the house that Jack built.  From there, the Sound instance affects the movie clip which, in turn, affects the audio portion of the video.  Set the volume, as shown, or pan.

11 Responses to “How to Adjust the Audio Portion of Flash Video”

  1. CMAX Says:

    short and sweet but useful , thanks ;)

  2. kweku Says:

    hi David,

    what if i want to control the volume with a slider? thanks

  3. David Stiller Says:

    kweku,

    That’ll be my next blog tutorial. :)

  4. Chad Says:

    thanks so much for this article!

  5. David Stiller Says:

    Chad,

    You’re welcome!

  6. rob watkins Says:

    thanks for this useful advice loading swf’s are easy this is just what i needed and it makes sense. well done. :)

  7. David Stiller Says:

    rob,

    Thanks!

  8. alex Says:

    Hi,

    Im trying to make a on/off sound button with the four lines of code.

    But it doesn t work……..

    here’s my code:

    Someone could help me with that please?

    btn1_bt.onRelease = function() {

    if(videoVolume.setVolume(0))
    {
    videoVolume.setVolume(50);

    }else{

    videoVolume.setVolume(0);
    }

    };

    thanks
    Alex

  9. David Stiller Says:

    Alex,

    Your if statement should be checking of the video’s volume is 0 — I’m almost certain that’s what you mean — but you’re using the Sound.setVolume() method (instead of getVolume()) inside your if statement.

  10. Chris Says:

    Hello David- is there a way to control the volume with a slider that is in a different SWF?

  11. David Stiller Says:

    Chris,

    The only thing trick to doing something like this with a loaded SWF is to know the object reference to the video player inside that SWF. If you use the Debugger panel, you can see all your objects in a hierarchical tree view on the left side … let should give you all the pathing info you’ll need. You’ll name the movie clip container that holds your loaded SWF, and then you’ll path do the instance name of the Video object or component that displays your video.

Leave a Reply