…the site you can never remember how to type…
A pretty common thing to do with Flash is to play an FLV file. In ActionScript 2.0, you would do the following:
1. Create a new Video object in your Library (choose New Video from the Library’s Options menu).
2. Drag the video onto the Stage, and give it an instance name.
3. Add the following code to frame 1 of your document:
// ActionScript 2.0var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.onMetaData = function(item:Object):Void { trace("metaData"); // Resize video instance. myVideo._width = item.width; myVideo._height = item.height; // Center video instance on Stage. myVideo._x = (Stage.width-myVideo._width)/2; myVideo._y = (Stage.height-myVideo._height)/2; }; ns.onCuePoint = function(item:Object):Void { trace("cuePoint"); trace(item.name+"\t"+item.time); }; myVideo.attachVideo(ns); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
That will play your video and also trace a couple cuepoints. Sample file: Load video with ActionScript 2.0
It’s kind of cool in ActionScript 3.0 in that you can open an empty AS3 FLA file, paste this code onto frame 1 of your document, and you’re off to the races (meaning, that’s all you have to do). As you can see, the code isn’t that much different either - so if you added video in AS2, things should seem pretty familiar. This is the same thing as above — it will also play a video and trace some cuepoints.
// 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 = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint}; video.attachNetStream(ns); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); 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); }
Sample file: Load video with ActionScript 3.0
This is Jen deHaan's latest blog in her neverending series of blogs. In the beginning there was deseloper. A designer developer. Now it's Flashthusiast, an enthusiast about Flash. Are you a Flashthusiast? Read on! This blog is about Adobe Flash, and will include tips, techniques and info on drawing, animation, and a bit on ActionScript 3.0. Jen is a QE on the Flash team at Adobe (San Francisco).
acrobatic
April 29th, 2008 at 2:58 pm
do you know how I can record video from my USB camera within flash? I’m able to connect to my camera, and see myself from within my FLA, but how can I save/record the video?
Cameron
June 9th, 2008 at 7:10 pm
Got it to work, but how do you get it to stop playing? I inserted that code on a specific frame to load a video and play. Once I leave that frame and go to another frame, the video keeps playing.