Loading and displaying video in Flash CS3 using ActionScript 3.0, no more video objects

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.0
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.
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

Author Jen deHaan

Jen deHaan is a freelance graphic and web designer, fascinated with great layout and usability. She has been working in the software industry since 2001, and has held positions with Macromedia, Adobe, and Motorola. Jen has written and contributed to over 20 print publications on web design that have been published by Peachpit, Adobe Press, and Wiley. She is also a professional pet blogger at DOGthusiast, and owns a small business called Stylish Canine.

More posts by Jen deHaan

Join the discussion 33 Comments

  • acrobatic says:

    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 says:

    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.

  • JJ says:

    I have the same problem as Cameron
    O_O

  • zmatt says:

    I am having that same problem with any video i try to encode with flash, the video sound will continue to play even though i change the frame and move on to another page.

    someone please help me!

  • Kleezy says:

    zmatt:

    Try this:

    ns.pause();
    

    To start it back up:

    ns.resume();
    
  • ds says:

    how would I go about adjusting the sound volume of my video in actionscript 3.0 ? ns.volume didn’t work? 🙁

  • Uday says:

    That’s really cool.
    Any one can tell me the use of :

    function ns_onCuePoint(item:Object):void {
    trace("cuePoint");
    trace(item.name + "t" + item.time);
    }
    

    Thanks,
    Uday

  • Jason says:

    Is it able to record video from webcam thru flash?? does anyone know the code fore it?

  • guy says:

    Lookign for the same : how to record , please

  • Jon1d says:

    cheers Kleezy so obvious and so true!

  • xerode says:

    To all those asking about recording webcam streams – you need special software running on a webserver before you can do that. There’s the official Adobe Flash Media Server or you can try the open source Red 5 – http://osflash.org/red5

  • Masa says:

    I tryed to load (swf) video and did not work , what I can do?

    ns.play("http://www.myste.com/video.swf");
    

    Many Thanks

  • brian says:

    You can also record video by using flash media server. It is quite straight forward. Check my site for some source. http://www.brianwiltshire.net/lab

  • beth says:

    you are a god – I looked for this info for hours…I wish google saw you sooner.

  • beth says:

    you are a god – I looked for this info for hours…I wish google saw you sooner.

  • I want to play flash file which was created in AS 2.0(named A). Can you please tell me how can we play this file by AS 3.0 file(named B).

    e.g.
    btn.addEventListener(MouseEvent.CLICK, playA)
    function playA(e:MouseEvent){
    //I want to play A.play();
    }

    If it’s possible please let me know.

  • http://www.zhoyosoft.com/ says:

    http://www.zhoyosoft.com/

    Plz help me ……

    Could u help me . In that site one hearder animation there . how will i get that kind of animation . do u know sent any link

  • zhoyofost,

    If I read your question right, the answer is this: The Flash Player either uses the virtual machine for AS2 or AS3, but cannot run both simultaneously.

    You can also try posting your questions on the support forums:
    http://www.adobe.com/support/forums/

  • susana says:

    Hi there,
    i’m trying to built a very simple website with flash cs3, I created three buttons, each one takes you to a different scene. one of the buttons takes you to a flv video,the problem is, when I press another button to go to another scene the flv keeps playing on and on. Is there any action script that I could add to make the flv stop when I press another button?
    thanks
    susana.

  • FlashSpec says:

    susana try this: video_obj.pause();
    put this in the onClick function of other two buttons.

    Here’s some flash of mine: http://bit.ly/CtVgvhttp://bit.ly/10Th0S

  • Fizheye says:

    is it possible to make this script loop the flv?

    // 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);
    }

  • Ann says:

    I am using the following code to load a SWF file into a “myClip” symbol on my main page. It is activated when “mybutton” is clicked and the child SWF file that displays is the same size as the original page and hence replaces it on the screen.

    var myLoader:Loader=new Loader()
    myClip.addChild( myLoader )

    mybutton.addEventListener(MouseEvent.CLICK, loadSWFfile);
    function loadSWFfile( evt ){
    myLoader.load( new URLRequest(“myFile.swf”))
    }

    It works fine but can anybody help me unload the file and return to the main page?

  • Niraj Vyas says:

    hi,
    how do i play video rss for eg. http://videos.oneindia.in/rss/news.rss
    i want to play Headlines with video and with it’s information and which will be automatically updating.

    need out put in flash

  • Niraj Vyas says:

    hi,
    how do i play video rss for eg. http://videos.oneindia.in/rss/news.rss
    i want to play Headlines with video and with it’s information and which will be automatically updating.

    need out put in flash

    Thanks,
    Niraj

  • zazzooooo says:

    Hey. Im having problems with a .flv i have uploaded in Flash CS4.

    Im making a website and i’m looking for a way to stop the video from playing when I navigate away from the video page.

    here is the code i have in frame one. the video i have in on the portfolio page.

    stop();

    function goAboutMe(e:MouseEvent):void{
    gotoAndStop(“AboutMe”);
    }
    AboutMe_btn.addEventListener(MouseEvent.CLICK, goAboutMe);

    function goPortfolio(e:MouseEvent):void{
    gotoAndStop(“Portfolio”);
    }
    Portfolio_btn.addEventListener(MouseEvent.CLICK, goPortfolio);

    function goGallery(e:MouseEvent):void{
    gotoAndStop(“Gallery”);
    }
    Gallery_btn.addEventListener(MouseEvent.CLICK, goGallery);

    function goMyServices(e:MouseEvent):void{
    gotoAndStop(“MyServices”);
    }
    MyServices_btn.addEventListener(MouseEvent.CLICK, goMyServices);

    function goContactMe(e:MouseEvent):void{
    gotoAndStop(“ContactMe”);
    }
    ContactMe_btn.addEventListener(MouseEvent.CLICK, goContactMe);

    Would be great if anybody has ideas on how i can fix this problem.

    thanks

  • zazzooooo says:

    just discovered a solution to the problem.

    http://forums.creativecow.net/thread/190/863350

    hope this is useful

  • vijay says:

    Hi,

    I have created a custom FLV player that plays FLV videos from the list. When say for eg video1.flv is playing at the moment and I select the another video say video2.flv then the new video starts playing along with its sound but I can also hear the old video’s audio also….Is there any code that I need to add to stop the old FLV audio from playing?

  • flyfly says:

    Hi, maybe someone can help me with this. I use this code(as3) to insert a video and it works,

    var conexion:NetConnection = new NetConnection()
    conexion.connect(null)

    var video:Video = new Video(672,475)
    addChild(video)
    var flujo:NetStream = new NetStream(conexion)
    video.attachNetStream(flujo)
    flujo.play(“myvideo.flv”)

    var theclient:Object = new Object()
    flujo.client = theclient

    but I need to erase the video once it has finished. How I detect the end of the video??
    Thanks!

  • WWilliam says:

    This is great but it does not fix the problem of having the video download on the background after you have clicked on something else.

    If you have a lot of videos and you click on them one after another you will saturate your internet connection. This only works if you only have one flv. There needs to be a way of cutting the video net-stream.

  • mahesh says:

    super brother………..

  • Thanks buddy. Not bad website you got here. Got some more links to point to with more information?

  • Matt says:

    Thanks!

    Do you have a full project source available ? I’m learning AS3 and want to see good examples and I have the idea that this is one.

    Thanks!

Leave a Reply