How to Adjust the Audio Portion of Flash Video
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.
April 18th, 2007 at 4:31 pm
short and sweet but useful , thanks
July 23rd, 2007 at 5:02 am
hi David,
what if i want to control the volume with a slider? thanks
July 23rd, 2007 at 7:49 am
kweku,
That’ll be my next blog tutorial.
September 16th, 2007 at 12:25 am
thanks so much for this article!
September 27th, 2007 at 10:10 am
Chad,
You’re welcome!
October 12th, 2007 at 1:34 pm
thanks for this useful advice loading swf’s are easy this is just what i needed and it makes sense. well done.
October 12th, 2007 at 1:45 pm
rob,
Thanks!
October 26th, 2007 at 10:30 am
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
October 27th, 2007 at 9:17 pm
Alex,
Your
ifstatement should be checking of the video’s volume is 0 — I’m almost certain that’s what you mean — but you’re using theSound.setVolume()method (instead ofgetVolume()) inside yourifstatement.December 11th, 2007 at 3:05 pm
Hello David- is there a way to control the volume with a slider that is in a different SWF?
December 11th, 2007 at 3:26 pm
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.