Recipe 16.6. Scaling
Video
Problem
You want to change the dimensions of the video
display.
Solution
Set the width and height
properties of the Video object. If you want the video to
playback at the dimensions at which it was encoded, use the
videoWidth and videoHeight values.
Discussion
The dimensions at which a video plays back are
determined by the width and height of the
Video object. When you construct a Video object, you
can specify the display's initial width and height; for example,
the following constructs a Video object that is 160x120
pixels:
var video:Video = new Video(160, 120);
However, you can still change the dimensions
after the object is constructed by using the width and
height properties. The following doubles the dimensions of
the Video object constructed in the preceding example:
video.width = 320;
video.height = 240;
The Video class also defines two
read-only properties, videoWidth and videoHeight,
which return the width and height at which the video was encoded.
You can use the videoWidth and videoHeight
properties to set the width and height of the
Video object, as follows:
video.width = video.videoWidth;
video.height = video.videoHeight;
However, note that the videoWidth and
videoHeight properties are not correctly defined until the
FLV has started to download. Therefore, if you want to set the
width and height of the Video object based on the encoded
dimensions, you must wait until the videoWidth and
videoHeight properties are correctly defined. You can use
a netStatus event listener for that purpose:
videoStream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
Then define the listener with an if
statement that tests for videoWidth and
videoHeight greater than 0, as well as width and height
values not equal to videoWidth and videoHeight.
Since the videoWidth and videoHeight are set at
the same time, you only need to test for one of them:
private function onStatus(event:NetStatusEvent):void {
if(_video.videoWidth > 0 && _video.width != _video.videoWidth) {
_video.width = _video.videoWidth;
_video.height = _video.videoHeight;
}
}
|