Posted Wednesday, April 18, 2007 12:15:36 AM by David Stiller
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 one 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
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.
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
ns with this movie clip.
Second, a new
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.
Category tags: Flash