The following example shows how you can load external FLV files using ActionScript 2.0 and ActionScript 3.0.
Full code after the jump.
The following example shows how you can load an .FLV file in ActionScript 2.0 using the NetConnection and NetStream classes and attach the video stream to the Video object on the Stage.
// ActionScript 2.0 /** * Requires: * - Video object on the Stage with an instance name of "video". */ var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.onMetaData = function (item:Object):Void { trace("metaData"); // Resize video instance. video._width = item.width; video._height = item.height; // Center video instance on Stage. video._x = (Stage.width - video._width) / 2; video._y = (Stage.height - video._height) / 2; }; ns.onCuePoint = function (item:Object):Void { trace("cuePoint"); trace(item.name + "\t" + item.time); }; ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); video.attachVideo(ns);
In ActionScript 3.0, the code is a little different. Instead of creating a new Video object in the Flash document’s Library panel, you can create a new Video instance using the Video class’s constructor. Also, note that instead of going <NetStream>.onMetaData = function(...);, you attach the onMetaData and onCuePoint event handlers to a client object.
// ActionScript 3.0 var video:Video = new Video(); addChild(video); var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.client = {}; ns.client.onMetaData = ns_onMetaData; ns.client.onCuePoint = ns_onCuePoint; ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); video.attachNetStream(ns); function ns_onMetaData(item:Object):void { trace("metaData"); // Resize video instance. video.width = item.width; video.height = item.height; // Center video instance on Stage. video.x = (stage.stageWidth - video.width) / 2; video.y = (stage.stageHeight - video.height) / 2; } function ns_onCuePoint(item:Object):void { trace("cuePoint"); trace(item.name + "\t" + item.time); }
11 Responses to Loading FLV files in ActionScript 3.0 using the NetConnection and NetStream classes
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising
" RT @OReillyMedia: #Ebook Deal/Day: Learning JavaScript Design Patterns -
$11.99
(Save 50%)
http://t.co/ilcmSDv6 " — pdehaan


perfect. thanks! just what i was looking 4.
gr8 ! thanks
Thank you! it seems that the live docs aren’t too clear as for the onMetaData event regards
Hi,
This way to trace the cuePoints works perfect. Now I am trying to play another video after the cuePoint on the first video is reached.
Any ideas?
can u tell me how will give a specified width and height?
thanks in advance
I’ve been looking for this the entire morning. Thnx so much!
I also found this for anti-aliasing the video: http://julian.empiregn.com/2006/1/30/Flash-Video-FLV-AntiAliasing
is there a way of inserting the video in a specific layer below the graphics i have?
thanks!
Thanks so much, now I finally got my CuePoints working. I’m a beginner and was looking for hours to find a working example for my task.
I have my website set up so that each video plays by clicking on a button with the following code:
/////////////////////////////////////////////////
player.source=”stream .flv”;
/////////////////////////////////////////////////
My videos stutter. I don’t think they are streaming. so I changed the code to:
/////////////////////////////////////////////////
var vid:Video = new Video();
addChild(vid);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play(“stream.flv”);
///////////////////////////////////////////////////////////////
This works but the videos continue to play when another button is clicked. You can hear the audio from one video while watching another.
How can I stop the video when another is chosen?
@Dennis,
Try calling
ns.stop()before callingns.play().Peter