How to Load External Flash Video (FLV) Files (AS2)

Flash ActionScript 2.0

Note:  This article has a companion piece, “How to Control Video (FLV) without a Component.”  An ActionScript 3.0 version of this article is located in a more recent entry of this blog.

It has been possible to load external video into a movie since Flash MX (aka Flash 6).  Loading FLV files, however, isn’t nearly as intuitive as loading other external media, such as SWFs, JPGs, MP3s, and the like.  The MovieClip.loadMovie() method, for example, loads an external SWF with a single line of code.  Not so for FLV.  So how is it done? 

Many people take the route of one of the Flash MX/Flash MX 2004 Media Components or the Flash 8 FLVPlayback Component.  These provide the convenience of drag-and-drop setup (just set the contentPath property in your Component Inspector panel) and UI controls for playing, pausing, adjusting volume, and more.  Certainly, nice features — but you may simply want a no-frills display, “Just give me the video.”  Besides, these Components only ship with the Professional editions of Flash and they do add to the SWF footprint.  The older Components weigh between 55KB and 68KB on their own.  The newer Component weighs less, but requires a Flash Player 8 SWF and still adds 33KB to your movie.

An answer, short and sweet

Let’s get to business, then.  Here’s a virtually 0KB way to add video to your SWFs, compatible back to Flash Player 6*.

  • Click on the Options menu of your Library panel (the square-ish icon in the upper right corner) and choose New Video….
  • Drag an instance of your new video object to the Stage.  Set its height and width as desired.
  • While the instance is selected, use the Property inspector to provide an instance name.  In this example, we’ll use videoPlayer.
  • Add the following ActionScript to a keyframe of the timeline that holds the video object.  If you like to be organized (and you do!), create a dedicated scripts layer, but any layer will suffice.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
videoPlayer.attachVideo(ns);
ns.play("externalVideo.flv");

* Important Note:  According to the ActionScript Language Reference, the NetConnection and NetStream classes require Flash Player 7 or higher.  In my own experience, I find success publishing to a Flash Player 6 SWF with ActionScript 2.0.

How it works

NetConnection is designed to provide playback of streaming FLVs from a local drive or HTTP address.  By supplying the NetConnection.connect() method with a null parameter, we avoid dependence on Flash Media Server (formerly known as Flash Communication Server) in order to play video.

NetStream handles the video stream for NetConnection, and our videoPlayer instance cooperates with all of the above via the Video.attachVideo() method.  Finally, our NetStream instance loads the external FLV via the NetStream.play() method.

The desired video will begin playing before the file completely loads — when enough data have buffered — so this is a great way to load sizeable video content without significant download time.

276 Responses to “How to Load External Flash Video (FLV) Files (AS2)”

  1. myIP Says:

    Why doesn’t the Video class have an attachAudio() and an adjustVolume method? I find it somewhat strange on how to adjust the volume of a FLV. Attaching the video using attachVideo() logically makes sense;

    VideoInstanceName.attachVideo(ns);

    Now attaching the audio to the FLV using the MovieClip’s attachAudio() is strange! I think it would make more sense if the Video class has an attachAudio() that we can use. But we are still not there yet to adjust the volume! We must now add a Sound object by doing the following;

    //controlling sound
    this.createEmptyMovieClip(”flv_mc”, this.getNextHighestDepth());
    flv_mc.attachAudio(stream_ns);

    var audio_sound:Sound = new Sound(flv_mc);
    audio_sound.setVolume(80);

    Weird? Or is it just me?

  2. David Stiller Says:

    myIP,

    Heh, well, I can’t answer your “why” question, of course. ;) I would love to know more about the in-house evolution of ActionScript. I imagine Colin Moock’s next book — whether it’s ActionScript 3.0: The Definitive Guide, Essential ActionScript 3.0, or whatever (all just conjecture on my part) — will illuminate some of that.

    My only guess is that the current API is an outgrowth of historical precedent. The MovieClip class existed before the Video class did. Sound has always been a part of Flash, and for reasons unknown to outsiders, the MovieClip class and Sound class were designed to be used in collaboration. Perhaps it would have been redundant, speaking from an under-the-hood point of view, to include audio members in the Video class.

    Some folks have a difficult enough time (at first) “getting” the relationship among NetConnection, NetStream, and Video: it already seems like jumping through hoops, right? But once it makes sense, it makes sense. In any case — not to wax too existential — it is what it is. Even if video isn’t as intuitive a pursuit in Flash as others are, I’m glad to have both video and audio in the API! :)

    And, as always, it’s easy enough write a wrapper class to tie these loose ends together.

  3. Mark Says:

    David — thanks for this great tip. I’ve spent the great part of the whole day attempting to work something similar, and here you’ve done it!

    Bravo, really. Quick question: is there a way to add some sort of listener in AS to see when the FLV is finished and send the playhead on to the next frame? Some kind of if/else statement?

    Thanks again for your blog!
    –Mark

  4. David Stiller Says:

    Mark,

    Great question! When I originally wrote this article, my intention was to follow up with a “part two” to cover what you’ve asked. I need to do a bit more research before I write a proper sequel, but I’ll tell you what I’ve toyed with so far.

    The Video class itself contains no events. (An event is a notification raised by an object when a certain incident occurs. An example of this is the Button.onRelease event, which fires after someone clicks a button then releases the mouse.) The Video class features plenty of useful properties and two methods — of which we’ve seen Video.attachVideo() — but no events. So that’s a dry well.

    NetConnection is out, too: only one method, nothing more. But NetStream provides what might be a promising event, NetStream.onStatus. More on that in a moment.

    Truth be told, I was hoping for, say, a NetStream.onComplete event, but something about video streams apparently makes the notion either too difficult or impossible. (Perhaps video streams simply cannot carry with them data on the expected duration of their content?)

    The next step, then, is to repeatedly compare the current position of the video against its length. But this isn’t a simple solution, either. Case in point: the Media class, which also plays video, states the following in its entry for the Media.totalTime property …

    Since the FLV file format does not provide its play time to a media component until it is completely loaded, you must input Media.totalTime manually so that the playbar can accurately reflect the actual play time of the media [ActonScript 2.0 Language Reference].

    The Media class happens to be the basis for the weighty Media Components discussed above, so I’d like to avoid its use in pursuit of as small a footprint as possible. That said, the Media class does provide a Media.complete event. I’m guessing it’s founded on the developer-supplied Media.totalTime property.

    Since the NetStream class provides a NetStream.time property, we do have a way to determine what the current position of the video is. If we use some repeating event, such as MovieClip.onEnterframe, or the above-mentioned NetStream.onStatus event, we could compare Video.time to a developer-supplied total. When the former is equal to or greater than the latter, the video has come to its end.

    I don’t much care for the apparent necessity of a developer-supplied total, because there may be cases where the video’s length is unknown beforehand — and then what? But it’s a start. In the sample below, the number 25 refers to 25 seconds and is completely arbitrary.

    
    // Load the video
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play("externalVideo.flv");
    // Monitor the video
    this.onEnterFrame = function() {
      if (ns.time >= 25) {
        trace("Video is finished!");
        delete this.onEnterFrame;
      }
    }

    Looking through the NetStream.onStatus event, I found that it does describe the video buffer (see the NetStream.Buffer.Flush code property). The case in which “Data has finished streaming, and the remaining buffer will be emptied” (ActionScript 2.0 Language Reference) may signal the end of a stream. Of course, it may also indicate network traffic, and that’s what I’d like to determine before publishing something “official” like a sequel. As soon as I figure it out, though, you’ll see an entry on it. :)

  5. myIP Says:

    “Perhaps it would have been redundant, speaking from an under-the-hood point of view, to include audio members in the Video class.”- Stiller

    I was also thinking the same. Yes, perhaps Moock will illuminate the subject in his next book. Thanks again David.

  6. zef Says:

    yay ive been needing this actionscript. thanx :p

  7. Andrew Ingkavet Says:

    Hi David,
    That was elegant. Only problem I can’t get video - only audio to show up. Working with Flash MX 2004 on a Mac. Any suggestions?

    thanks in advance
    andrew

  8. David Stiller Says:

    Andrew,

    I was successful with this approach in both Flash MX 2004 and 8. Granted, I only have Flash for PC. Are you publishing for Flash Player 6 or higher?

  9. Diane Pfefferkorn Says:

    I’ve been working with Captionate to include closed captioning with my Flash Videos. This has required me to load an external FLV using AS. All this works fine, the captions appear and they are synched with the video but I’m having trouble incorporating a Video Controller. Since my videos load after viewer presses “launch video” and then play automatically, I’d like the video to loop back to the beginning and then stop at the opening scene as it would using the FLV Playback Component. Can you show example of Actionscript that will meet my requirements of having a Playback Control with an external video, or direct me to information that can help. I have about 50 videos to caption, so I’d appreciate any help you can give. If you email me directly I can provide my source files. Thanks!!!!! - Diane

  10. David Stiller Says:

    Diane,

    The FLVPlayback Component is even easier to use than the approach described in the above article. If you don’t mind the overhead of this Component — which, at 33KB, isn’t as bad as the older Components (some would argue isn’t bad at all) — and if you don’t mind publishing for Flash Player 8 or higher, just drag an instance of the FLVPlayback Component to the Stage, select the Parameters tab in the Property inspector, and set the contentPath parameter to a path reference to your FLV.

    You can handle this via ActionScript by giving your Component instance an instance name, then using that to invoke the contentPath property of the object.

  11. Andy Says:

    Hello,

    I was wondering how to get FLVPlayback component working with NetConnection and NetStream?

  12. David Stiller Says:

    Andy,

    Actually, FLVPlayback takes care of all that for you. See my reply to Diane, above … all you need to do is set the contentPath parameter of that Component. Unless maybe I misunderstood your question?

  13. Andy Says:

    Hello David,

    Thanks for your reply. I’m sorry I should have added some detail:
    What isn’t clear to me in the F8 language reference regarding NetConnect, NetStream and the FLVPlayBack component is whether setting contentPath for FLVPlayBack is enough to ensure that my .flv video is being streamed to the the component. Or is there some undocumented Actionscript to connect a NetConnect and NetStream instance to FLVPlayBack? Is streaming even possible in the scenario or is it simply progressive download unless the URL I provide in contentPath is from a streaming URL via Flash Media Server?

    Thanks again,

  14. David Stiller Says:

    Hey, Andy,

    Yeah, FLVPlayback handles the simulated streaming. That’s one of the benefits of using Components: they’re designed to do the heavy lifting for you. In your case, just set the contentPath parameter, either via the Property inspector or ActionScript. While this will only give you progressive download — unless you’re using Flash Media Server — it really does feel like streaming to the end user.

    Keep in mind, of course, FLVPlayback requires that you publish for Flash Player 8 or higher. And it’s only available in the Pro version of Flash, while the approach discussed above can be done in Flash Basic.

  15. Gary Says:

    David,
    We have Flash Pro 8 and we are running a .FLV movie. Using the FLVPlayBack we set the contentPath to filename.flv. Also, reading the above post with Andy and Diane we removed the AS code that you described in this article. But the problem we have is viewers are complaining that our video is “choppy” - meaning it starts and stops. As a side note - We are hosting this on a shared server. What are we doing wrong?

    Thanks

  16. David Stiller Says:

    Gary,

    There are several factors that determine the smoothness of a video’s playback, including overall size (its dimensions), bitrate and sample rate (of both the video and audio streams), number of channels (mono or stereo), compression, number of keyframes — even the visual characteristics of the content itself (lots of motion versus very little). Add network traffic to the mix, and there you have it … converting video for web use is as complex a discipline as anything else. ;)

    You may not be doing anything wrong. You should consider the above facets, though, and experiment with reducing the dimensions, reducing the bitrate, etc., until you produce something as small as reasonably possible, without an unacceptable loss in visual quality. It’s the same balance you must strike with, say, JPGs — but a greater number of properties to fiddle with.

  17. JustKaz Says:

    I am trying to make a button, and use it to load an external .flv into the FLVPlayback - Player 8 component.
    I am not all that professional with flash yet.
    Would love to be able to load it from xml file.
    If you have an idea, or the knowledge, please post, and please include an example .fla and .xml if possible in zip or rar format as it may save tons of conversation as I have already stated, I am no Flash guru. The reason I need to learn this is I find I have a need to be able to create a .flv player that the clients can pick from many .flv movies to play the one desired. JustKaz

  18. David Stiller Says:

    JustKaz,

    I’ve got to chuckle. This article is geared away from the FLVPlayback Component, but all the recent comments are geared toward it. Maybe I should write an entry about FLVPlayBack. :)

    What you’re after is certainly doable, but it really merits a tutorial to itself. I see at least four sub-goals, here: a) loading XML into Flash; b) using the loaded XML’s data in ActionScript; c) loading an external FLV into the FLVPlaybackComponent, which has been covered in other comments here; and d) doing the above at the behest of a button, which is probably the easiest part.

    I’ve been asked to write an article or two on XML in Flash, and the topic is definitely on my list. In the mean time, here’s a good start.

    http://www.kirupa.com/web/xml/index.htm

    Remember, big things are made up of lots of little things. Don’t try to swallow the elephant all at once. ;)

    Of course, one extremely simple solution — thinking outside the box, here — is to choose a particular name for the external FLV and hard code that … then tell your clients to rename their FLVs, in turn, to that name.

  19. Andy Says:

    Hi David,

    Thank you for your reply. I understand that the article was geared away from FLVPlayback but it seems so silly that there is not simple AS to connect FLVPlayback to a “real” stream, not progressive download.

    Is there a way to connect MEdiaDisplay or FLVplayback to a stream using NetConnect and NetStream?

    Thanks again,
    Andy

  20. David Stiller Says:

    Andy,

    I may not have been clear on this — apologies, if not :) — but to be explicit, true streaming cannot occur without Flash Media Server (or some other media streaming server software, if such a 3rd party solution exists for Flash). Once the server software is installed and configured, the ActionScript hookup is as easy as it was before: simply point it at the stream, rather than a file.

  21. Brandon Says:

    David, I’ve spent a whole day try to figure it out and you made it seem so simple! Thanks.

    My question is, how can i get flash to loop my flv video?

    I’m using Flash 8

    Thanks,
    Brandon

  22. David Stiller Says:

    Brandon,

    Check out my reply to Mark’s comment (April 16, 2006). Using either the MovieClip.onEnterFrame or NetStream.onStatus approach, you could call the NetStream.play() method again when the video finishes. Does that make sense?

    This FLV entry is one of my most popular articles, so I should probably write a follow up or two. Write back if this doesn’t answer your question. :)

  23. maharaja Says:

    Hi David,
    thanks for the info that u have shared . I have a doubt regarding the progressive streaming could you give a snippet explaining the progressive streaming.

  24. David Stiller Says:

    maharaja,

    I’m afraid I don’t understand your question. There are two concepts in discussion so far: streaming and progressive download. Streaming requires server software; progressive download does not. What sort of snippet are you looking for?

  25. maharaja Says:

    i want example in which it uses progressive streaming
    i tried the following code:
    //**** the code is in Flash 8
    nc = new NetConnection();
    nc.connect(null);

    ns = new NetStream();
    my_video.attachVideo(ns);
    ns.bufferTime(0);
    ns.play(”test.flv”);
    //****
    but according to my understanding the slider should not be accessible till video is completely loaded but in this case i can play with slider as soon i play the swf… may be i think that size of file is small so that it might be loading quickly . size of test.flv is approx 7 MB would u please put some light thanks in advance

  26. David Stiller Says:

    maharaja,

    This article addresses the display of video without the use of Components — with a Video object only (and some ActionScript) — so I’m not sure what you mean by “slider”. Are you talking about one of the Media Components, or FLVPlayback?

  27. Eric Says:

    David,
    thanks for making this information so readily available…it’s brought me much closer to understanding how to achieve what I’m trying to do…I’ve still got one more question though…

    So I’m stumped on where I should locate my .flv file in relation to the final .swf that targets it. Does it matter? should I include the full path in my “ns.play(”externalVideo.flv”);” line? Do I have to upload my videos to the web first to test it out, etc etc etc.

    thanks for your time!

  28. Dan Says:

    Hi David,

    Thanks for the article… it solved about 1/2 of my problem. I have about 30 flash videos, but want to make a single swf player, and just send the particular video filename to play directly to the swf player. No problem there, it’s working with your script. But, now I don’t have a way to control that video. I’m using Flash MX 2004 on a mac, so I can only publish to flashplayer7.

    I know this isn’t the point of your article here, but I was hoping you could help. I’ve tried dragging the MediaPlayback component over and hooking it up to the video, but it doesn’t work. Any suggestions?

    Thanks!

    Dan

  29. Brandon Says:

    Thanks for your help….I’m not quite sure i understood what you meant (sorry, i’m new to flash and am not familiar with ActionScripting).

    I entered the code to monitor the movie and then replaced the ‘trace’ with the NetStream.play() method you mentioned. It didn’t work…but that may not have been what you meant. Here is what my code looks like right now.

    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(”772-2_l.flv”);

    this.onEnterFrame = function() {
    if (videoPlayer.time >= 20) {
    ns.play(”772-2_l.flv”);
    delete this.onEnterFrame;
    }
    }

  30. David Stiller Says:

    Three replies to the previous three questions.

    Eric,

    The location of the FLV, SWF, and HTML document are all significant, especially if you’re using relative paths, as demonstrated above. If you’re using absolute paths, then yes, you’ll have to upload your FLVs to the server in order to test — because that’s there the absolute path points. There is a way around that, so take a look at (Perhaps) Unexpected Point of View: SWF Defers to HTML for a more detailed explanation.

    Dan,

    I have about 30 flash videos, but want to make a single swf player, and just send the particular video filename to play directly to the swf player.

    For that, if I understand you correctly, you’ll have to set up your path, as referenced in the line as a variable, then feed that variable to your SWF from the outside. There are two basic ways to handle this passing-in of variables: 1) append a query string to the SWF reference in your object param and embed elements, and 2) use FlashVars. From a practical standpoint, both approaches amount to the same thing.

    Again, if I understand you, you want a single SWF to be able to load 30 different FLVs, and you want to be able to externally instruct the SWF which FLV to load. Did I hear you right?

    Brandon,

    You did fine — this was my mistake (thanks for catching it!). It’s not the Video class that provides a time property, but rather, the NetStream class. I amended my reply to Mark with correct ActionScript. In your code, the only thing that needs to change is the reference to videoPlayer.time: change that to ns.time. Make sense?

  31. lijo Says:

    hi David,
    Iam using Flash 8 proffessional version.and not god in actionscript.. just began in it..iam doing a website now..In the website i have used a Mediaplayback component to play a flv..(not a flvplayback component).
    flv file is loading from a folder..I uploaded the website..but the flv file playing by step by step..i meant loading little bit then playing..loading then playing..

    i want to load it fully first then have to play smoothly.. iam also want to show a loading message in the time of loading..just like “loading…”

    i tried a code ——-

    onClipEvent(enterFrame){
    _root.mymedia.autoPlay = false;
    }

    —–then it loading first but does’nt play automatically..i need to press play button :(

    this webpage for a portfolio for a company..First impression is the best impression right?:) ..When a customer is opening this page they will not see a jerking video..If iam showing a “loading…” message, they will wait to see and it will play smoothly..

    what is the soluiotn David?
    Thanks

  32. David Stiller Says:

    lijo,

    Preloading a video is not unlike preloading a SWF or JPG. Because you’re using one of the Media Components, your first stop should be the Media class entry in the Components Language Reference. Note the Media.bytesLoaded and Media.bytesTotal properties under the Properties summary. Those properties will give you comparable information to the MovieClip.getBytesLoaded() and MovieClip.getBytesTotal() methods described in How to Tell When an External SWF has Fully Loaded.

  33. Markus Says:

    Hello, I just get the audio, no video. :(
    What can I be doing wrong?

    This is what I got:
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    FLVPlayback.attachVideo(ns);
    ns.play(”2.flv”);

    I’m using a PC with Flash Professional 8.0.

  34. Markus Says:

    Btw, everything works when I put a value for contentPath in the “Component Inspector” but I need to be able to change the content dynamicly. I’m going to have one player that can open different files depending on what data it receives (ex: www.mysite.com/player.swf?id=123). But I can’t get it to work when we have to set the contentPath value in a actionscript.

  35. David Stiller Says:

    Markus,

    I’m confused by your example code. The attachVideo() method is defined by the Video class, but you seem to be invoking it as a static method of the FLVPlayback class. According to the documentation, the FLVPlayback Component “wraps” a VideoPlayer instance — so I wonder if you’re somewhat “mixing metaphors,” here.

    If you want to use a FLVPlayback Component instance, all you have to do is give that Component an instance name and use the instance name to reference its contentPath property. You don’t need any of that NetConnection or NetStream business.

    On the other hand, if you want to forego the Component — which is the focus of this article — you use the ActionScript I show at the very top along with a Video object.

  36. hemanshu Says:

    Very good for learning purpose.

  37. Markus Says:

    Ah, yes I got it now. Thanks alot mate! :D
    Hadn’t quite understood how all this worked, but its running smoothly now!
    Thanks alot for your time David! Well appriciated! :)

  38. David Stiller Says:

    Glad to hear it, Markus! :) Enjoy.

  39. David Stiller Says:

    hemanshu,

    Thanks!

  40. Markus Says:

    Hi agian! :)
    When I load a new .flv-file with

    videoPlayer.contentPath = _root.id + “.flv”;

    my navigationbar won’t show up. This worked fine when I had a value for contentPath written in the Component Inspector, but not now when I load a contentPath with the actionscript. Have you any idea if I need to reload the navigationbar or something like that after I have loaded a (new) contentPath.

    (My goal is to have a player that can load several different .flv-files. www.mysite.com/player.swf?id=1 opens 1.flv from the same dir. That works fine now, but when I do it like this, the navigationbar won’t show up anymore.)

    Don’t know if it’s called navigationbar but couldn’t find any name for it.(The bar with the timeline and volume etc.) :)

  41. David Stiller Says:

    Markus,

    I’m guessing you haven’t provided the skin in the Component Inspector. You can use both approaches, by the way: select the skin in the Component Inspector and set the contentPath via ActionScript. Or set both via ActionScript. Look up the FLVPlayback class in the Components Language Reference to see what all your choices are. Components are objects, just like anything else, and their classes follow suit, as described in my Objects: Your ActionScript Building Blocks article.

  42. Ryan Says:

    David, thanks so much for this code. I’ve read this whole thread and can’t seem to find what I need to modify… I am trying to use FlashVars so I can use one swf to play several different flv files. I have my embed and param tags set up correctly but I can’t figure outy what I need to put into your code…

    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(”externalVideo.flv”);

    I tried changing it to this:
    ns.play(”openmediaPath”);

    openmediaPath is my variable I define in the html. Am I using the wrong syntax?

  43. David Stiller Says:

    Ryan,

    You’re allllmost there! Notice that your openmediaPath reference — which I assume is the variable you’re passing in via FlashVars — is quoted. Those quotes are killing it. You want to reference the value of openmediaPath, not the string “openmediaPath.” Get it?

  44. TJ Says:

    Hi Dave.

    It looks like you have been very helpful to a lot of people, I think that is very cool. I hope that you can help me as well…

    My issue seems to be strictly with absolute vs. relative paths.

    When I use your code in my FLA and preview it in my browser it works like a charm. However, if I try to just view the SWF by itself OR even create an EXE, it doesnt work.

    BUT, if I use an absolute URL to the FLV then it works in all scenarios. The problem is I am developing something to be deployed on CD-Rom. I will not be able to use an absolute path (nor do I think I should have to) to the FLV.

    Any help is greatly appreciated…. TJ

  45. David Stiller Says:

    TJ,

    We’ll have to do a bit of troubleshooting, here. I pasted the exact code in my article into a new FLA with an external FLV in the same folder as the published SWF. Worked fine. Then I published a Windows EXE into the same folder and it continued to work. What version of the Flash Player are you publishing to? Have you tried this with another FLV?

  46. TJ Says:

    WOW! Thanks so much for the quick response.

    I just completed some more testing myself and noticed something…

    Originally I was using Flash Pro 8, and publishing to Player 8. I noticed the security settings in the publish settings tab about local vs. network access and neither of them seemed to improve the outcome.

    HOWEVER, I tried the same code with the same FLV using Flash MX2004 Pro, and publishing to Player 7 and it worked (almost). It found my FLV but only played audio… not video. I recreated my FLV using Flash MX2004 and it worked. I assume this was a codec issue with my original ver8 FLV.

    The fact that I can get what I need with 7 is going to work with my current situation. But, I am definitely interested in trying to figure out what is going wrong with 8. I have tried a couple of different FLV’s and I also tried diff AS code (using the sample code from Flash 8 ). Again, it is always the same problem… preview in browser - no problem. SWF by itself or as EXE, it cannot find the FLV?

    I really appreciate your assistance. Please let me know if there is anything else you need from me to help figure this bad boy out.

  47. David Stiller Says:

    TJ,

    WOW! Thanks so much for the quick response.

    That’s luck of the draw, I’m afraid. Sometimes I get overwhelmed with other things — but I’m glad I was able to respond quickly this time, too. ;)

    I’m interested in your problem, because none of this rings a bell with me. I didn’t have this issue in Flash MX 2004 and haven’t had it in Flash 8. I’ll try to help you if I can, but I need a bit more detail: are you on Mac or Windows? Write me offline — my last name @ quip.net — and I’ll take a look at your code. I’m on WinXP.

  48. David Stiller Says:

    Note: For anyone following along, I took a look at TJ’s files. They work just fine on my machine, so TJ and I are baffled. This seems to indicate there’s something wrong specifically with TJ’s computer, security settings, Flash preferences, or some other TJ-only circumstance.

  49. Danish Says:

    Hi David,
    Nice work!
    I really appriciate what you are doing not only with flash but with helping ppl.
    Now even im stuck up at one thing. I hope you can help me with this.

    Im trying to buid a flv player which will pick movie file at runtime( like most ppl are doing) im using FlashVars its working good, but the trouble is that i want the seek bar to be able to point to prev sections and play from an earlier location ( something like youtube ).In my instance the slider doesnt appear unless the movie is completely played once. Im plainng to upload movies upto 30 mins duration and it would be difficult then. So do u have any ideas of how to make this work?

  50. David Stiller Says:

    Danish,

    Thanks. :)

    The trouble with what you hope to accomplish is that it isn’t possible to seek to sections of the video that haven’t yet loaded — unless you’re streaming the FLV from something like Flash Media Server. In my experience, the slider appears as soon as enough data have loaded … the FLVPlayback Component indicates load progress right in the slider, and it is indeed possible to seek among loaded frames. Are you using the FLVPlayback Component?

  51. Wayne Brady Says:

    Is it possible to load a FLV via nc.play(”file”) to a flvplayback component? I ask because there are methods i can use, such as seekToNavCuePoint with the component, whereas I can’t with NetStream. There’s more pros and cons as well. Any ideas?

  52. Danish Says:

    Hey David,
    thanks for the help. it works, it was my mistake i wasnt putting the slider compnent correctly. but it works good now.

    Although i was trying to compare the playback with you tube and was trying to find the best combination of bitrate and fps for encoding to flv, mine was always jerky. Then i ripped a video off you tube itself and tried to play that on my flvplayer and it was a lot better. The only thing is that it looks lower in quality (like antialiasing is turned on you tube and its off in mine). So this leads to anothr set of questions,
    - What should be the optimal conversion parameters that the video plays decent enough?
    - How is it possible that same file playes smooth on youtube’s player and looks grainey on my player?
    - What possiblities does meta header hold, can this be credited to meta headers?

    I know this is out of the topic … i can mail u if u want to discuss this outside.. :)
    Thanks again and keep up the good work.

  53. David Stiller Says:

    Wayne,

    If you’re using the FLVPlayback Component, you can ignore the NetStream / NetConnection combo. :) FLVPlayback uses those classes along with additional code to handle the basics for you. That’s definitely one of the many benefits of using the Component, if you don’t mind the larger SWF file size and the requirement to publish to Flash 8 or higher. Just set the contentPath property of your FLVPlayback instance (via the Component Inspector or ActionScript) and you’re set.

  54. David Stiller Says:

    Danish,

    Glad you got it working! :-D

    Consider this article at Community MX, by Tom Green and Scott Fegette (free content):

    FLV Datarate and Bandwidth… Demystified

    You may also want to look at Scott’s Breeze seminar at the Adobe Developer Center:

    Producing Video for Web

    Between the two of those, you should find lots of useful information, even if it doesn’t answer every one of your questions.

  55. Enrique Iniguez Says:

    Thanks for the lines of actionscript sure does load quick.

    I wonder how I can add the play, pause, stop buttons etc….
    along with this actionscript.

    Thanks Again
    Enrique

  56. David Stiller Says:

    Enrique,

    You’ll be glad to hear that your goal isn’t hard at all. In fact, that would make a great follow up to this article — so I’ll write about that in the next few days.

    For now, take a look at the NetStream class. In the example code, the variable ns refers to a NetStream instance. Methods are things an object can do (such as play), and you’re already using the NetStream.play() method. Check out the methods summary for this class in the ActionScript 2.0 Language Reference and you’ll see others useful to your goal. But again, I’ll write about this in an actual post very soon.

  57. Andrew Ingkavet Says:

    Hi David,
    You were so kind to reply to me back in April. Just wanted to let you know it was a MIME type issue of the FLV type not being recognized. Thanks a lot.

    Thinking aloud, I guess I could do a URL-encoded variable in a link to load different FLV files with the one container swf, yes?

    This would allow me to have a simple page with one SWF (video container) and a page of HTML links with a variable in the link such as 300monks.com/?video=artist1.flv

    best
    andrew

  58. David Stiller Says:

    Andrew,

    Ahh, those MIME types! Glad you resolved your issue. :)

    You could certainly use a name/value pair to send information to the SWF. See this article for one example. Your suggestion is a bit different, in that your information is passed to the HTML document, rather than to the SWF itself. You’d need to use JavaScript to grab that query from the address, then pass it in via FlashVars, but it amounts to the same thing. Have fun with it!

  59. sank Says:

    hi david
    thanks for a concise tute. clean and fun.

    one question: can cue points with buttons to call those points be set using the video object with ns?

    i know it’s a breeze with the FLVPlayback component and associated playback behaviors extension… but can it just as easily be built into your example with actionscript?

    the reason i ask is i’m using a custom-built player with a certain shape/look… so using the FLVPlayback component isn’t an option…

    -unless i’m mistaken & one can infact use the FLVPlayback component with custom buttons (& i don’t mean simple skinning ;)

    thanks!
    s

  60. David Stiller Says:

    sank,

    one question: can cue points with buttons to call those points be set using the video object with ns?

    I think you’re asking what Enrique did, recently. Have you checked out this articles companion piece, “How to Control Video (FLV) without a Component“? I think that gives you what you’re after. :)

    -unless i’m mistaken & one can infact use the FLVPlayback component with custom buttons (& i don’t mean simple skinning ;)

    The FLVPlayback Component has an API, just like the NetStream class, so you can, in fact, manipulate it independently from its built-in controls. Just check out the FLVPlayback class in the Components Language Reference and note the available methods (things it can [be told to] do) and events (things it can react to). But my hunch is that you’ll actually want to manipulate the NetStream instance instead.

  61. smavi Says:

    Hi,
    I am trying to find/code an interface like MediaPlayback (with the buttons and the bar etc.) to play a .swf movie clip. I don’t have .flv files but a bunch of
    .swf movie clips and I am trying to have a nice interface to play them in.
    Everything I searched and found so far is about playing .flv files :(
    Any ideas/pointers will be highly appreciated.

    Thanks!

    Esra…

  62. David Stiller Says:

    Esra,

    I like your idea, but consider this: FLVs are linear … they have a starting frame and a final frame, and the idea is to move from the beginning straight to the end. SWFs can be linear, but many (perhaps most?) aren’t. SWFs, for example, have nested clips with their own independent timelines. SWFs may jump from a later frame to an earlier one, or pause while ActionScript handles a bit of animations — or the entire SWF might be animated by ActionScript. So how should a MediaPlayback-like interface work?

  63. Jack Says:

    Hi,

    I have installed fms2 in my local pc and sucessfully stream the flv .
    Now my problem is I do not able to create seek bar .
    I load the flv like given [videoPlayer.attachVideo(ns);]
    How to create a seek bar ? Any suggestion ?

  64. Daniel Says:

    Hello dave, i have a question. Im developing a video player in flash, but what i want is that when it’s open, it doesn’t download til the user clicks the play button. Is this posible??

    Best regards

    Daniel

  65. The Freaking Dutchman Says:

    David,

    Is it possible to use cuePoints when playing my FLV without a MediaPlayer or FLVPlayback component?
    If so, can go give a little AS hint?

    Thanks

  66. David Stiller Says:

    Replying to the last three …

    Jack,

    I have very little experience personally with Flash Media Server, so I’m afraid I don’t have a ready answer off the top of my head. A seek bar, conceptually, can be compared with a preloader bar. What you need are two values: the total duration of the video, and the current location of the video (that is, how far into the file the video has played). I believe FMS does provide total duration (progressive download does not — you’d have to supply the value yourself, as even the FLVPlayback Component requires). The NetStream class does provide a time property. Running these two numbers against each other gives you a ratio that you could then use to position a “handle” movie clip against a “track” movie clip. Not a bad idea for an article on this blog, actually. ;) It’s on my list.

    Daniel,

    When you say “the play button” (emphasis mine), I wonder if you’re using something like the FLVPlayback Component, which has a built-in play button. If so, you’d have to do a bit of fancy programming to override some of the FLVPlayback’s default behavior. If you’re using the bare-bones approach described in the blog article above, you could assign the ns.play("externalVideo.flv"); to the Button.onRelease event handler of a button — that would do it.

    The Freaking Dutchman,

    It should indeed be possible to roll your own cue points. :) Given the NetStream.time property, comparable to the Sound.position, you might experiment with the same approach outlined in my recent Cue Points for Audio Files article on the Adobe Developer Center.

  67. Jeff Says:

    David,

    I’m new to the actionscripting in video…after reading all the article posted here… i had tried them and it work fine to me, just that there is one more thing i can’t get it… how to load different flv into the same swf file?

    Can it be done using like,say, after the first flv, go to next frame? please guide me a little.

    Thanks

  68. Jeff Says:

    David,

    sorry to post two time…just to tell that i came out a solution already. here is what i have done. to me it’s work fine. If anything that can be improve pls correct me:

    var vidscreen:Video;
    var mync:NetConnection = new NetConnection();
    mync.connect(null);
    var myns:NetStream = new NetStream(mync);
    vidscreen.attachVideo(myns);
    myns.play("01.flv");
    myns.onStatus = function(infoObject:Object) {
      switch (infoObject.code) {
      case 'NetStream.Play.Start' :
      case 'NetStream.Buffer.Full' :
        vidscreen._width = vidscreen.width;
        vidscreen._height = vidscreen.height;
      }
      if (
        infoObject.level == "status" &&
        infoObject.code == "NetStream.Play.Stop"
      ) {
        myns.play("02.flv");
      }
    }

    but now facing a problem… how to make the flv played according to vertical center…because all the movie not in the same size and also my video object…anyone can do it please guide me…thx

  69. David Stiller Says:

    Jeff,

    That fact that you’ve come this far on your own should be an encouragement. :) I’m not exactly sure what you’re after, but it looks like you’ve got a good start. The NetStream.Play.Stop message appears when playback has stopped. You should do some experimenting to determine if this only happens when the file has reached its end (the video is over) or if it also happens when network congestion causes the video to temporarily pause while buffering. You only provided instruction for one of your two case statements, which means the resizing will occur both for the NetStream.Play.Start and the NetStream.Play.Full messages — not sure if that’s what you want, but it might be. If you’re resizing like this, I don’t see why you’d need to vertically center. Can you explain?

  70. Jeff Says:

    well appearently i was making a fullscreen flash that run different movie sizes(example 1024×819, 1024×780)… well i got no problem with x size since all are in 1024…but because of y size not the same, i can’t make a “set” size for the video object…that’s why i was hoping to see if there is a way to make it play in the center(because my video object is 1024×768)

    Anyway after i dig more into the tutorial while you are not around(since we are in a different half of the world)… i somehow found a solution to it…(and this previous script i posted seems to have some problem too)…

    I have placed the files in www.geocities.com/zaslone
    movie01.fla & movie.swf - the flash files(place flv named 01-05 will make
    the swf work.
    netstream.zip - flash files with sample flv included
    the swf created can actually play the flv and when it finished it move to nextframe which another movie is loaded…

    And if they can’t find the file, they will go back to first one(still haven’t think of how to make them skip to next frame instate of going back to beginning)…anything can be simplified pls tell

    Anyway, thanks for guiding me. :) …and sorry for a long post…

  71. David Stiller Says:

    Jeff,

    No worries about a long post. Sometimes my replies are pretty long. ;) You should be able to adjust the position of the Video object itself by adjusting its _x and _y properties. Generally speaking, the way to center something is to take the width (or height) of the parent object, divide that by 2, then subtract the width (or height) of the desired object, also divided by two.

    e.g.

    video._x = container._width / 2 - video._width / 2;

    When you say this …

    still haven’t think of how to make them skip to next frame instate of going back to beginning

    … I don’t know if you mean video frames or movie clip timeline frames. If you mean timeline frames, just invoke the MovieClip.gotoAndPlay() method (or gotoAndStop(), etc.).

  72. zignorp Says:

    Hello! and thanks.
    I’m building a custom class in as 2.0 that does a few things, and one of them is to set the contentPath of an existing FLVPlayer component instance. If I’m loading a movieClip, it’s easy to declare a MC variable in a method in my class file, then refer to it in my flash movie, but I’m baffled on how to refer to or type a var for the FLVPlayer component instance in my class file. I don’t really have to do it this way, but would like to know how!

  73. David Stiller Says:

    zignorp,

    How are you writing your class file? Are you extending the MovieClip class? If so, then your reference to the movie clip would be this — which is easy, as you’ve described. If not, then your reference to a movie clip would simply be its instance name, presumably passed into your class instance via the constructor or some other method (just as easy). In like fashion, you could either extend the FLVPlayback class or reference its instance name in the same way (this approach is called composition). Have you tried that? I’m not sure where you’re getting stuck. :)

  74. Jeff Says:

    David,

    thanks for the example you gave…I applied on it and it work just as i wanted now…

    about this:
    still haven’t think of how to make them skip to next frame instate of
    going back to beginning

    what i mean here is,like, if my timeline 4 frame. each of them have one different flv played(example 01.flv,02.flv,03.flv,04.flv)

    lets say i forgot to put in 02.flv but i got 03 and 04, if i told flash to gotoAndPlay(1) if no flv of this name is detected(02.flv) …then they will skipped all the flv at the back(03.flv, 04.flv)and go back to frame 1.

    i wonder if i wrote the script like, on the middle frame(frame 2, 3)

    if (infoObject.code == "NetStream.Play.StreamNotFound") {
      nextFrame();
    }

    and putting this in the end of timeline

    if (infoObject.code == "NetStream.Play.StreamNotFound") {
     _root.gotoAndPlay();
    }

    hope you know what i’m saying… :|

  75. David Stiller Says:

    Jeff,

    Aha, now I see what you’re doing. Well, honestly … I don’t see much benefit in using timeline frames to load your videos. That adds unnecessary complexity, don’t you think? Why not just use NetStream.play() on your existing myns object to load another FLV when the first one finishes? That’s the great thing about these objects: you only have to instantiate them once — after that, just keep using the same objects. Does that make sense?

  76. Jeff Says:

    well i do try that at the first place…using one frame is that what you mean… some how that time i can’t get the things work well…i will try to give it another try…again thanks for guiding :P

  77. zignorp Says:

    Thanks David!
    I am accessing my target movie clip through a method, which I’ll paste below.
    I’m not sure what type to assign to the FLVplayer object, would this be a MovieClip as well?

    function loadFilm(filmName:String, targetMC:MovieClip, filmID:Number, smFlv:String) {

    smFlv = “flv320/”+filmName+”.flv”;
    descSWF = “swfs/”+ filmName+”.swf”;

    //playerFLV.contentPath=smFlv;
    targetMC.unloadMovie();
    targetMC.loadMovie(descSWF);

    }

  78. zignorp Says:

    I just got it to work by extending the MC class AND adding it as a parameter of the function. I’m not sure why I had to do both, as the other target MC was working fine, but thanks for your help and education!

  79. David Stiller Says:

    To Jeff …
    You got it. One frame is all you need. If that NetStream.Play.Stop message is correctly indicating the conclusion of a video — and you’ll really have to experiment to determine that — then you can simply invoke NetStream.play() against the ns instance. With your approach, you’re essentially doing the same thing, except you’re completely re-instantiating that ns object, along with the NetConnection instance.

    To zignorp …
    Glad you got it! I, too, am surprised that you had to do both, but hey … it’s working. :)

  80. Aaron Says:

    i love you, i’ve been looke everywhere for a stright foward solution to play flv’s without components, i thought i would never find anything. thanks for the help man it was much needed.

  81. David Stiller Says:

    Aaron,

    Glad to hear it! Thanks!

  82. Chris Says:

    I am using Flsh8 and was using the FLVplayback component but no video is present when using a web browser to connect to site, however when I open file using ftp client it works fine. I am using a relative path to call the .flv from .swf I tried the
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(”externalVideo.flv”);
    and the same thing, any ideas what i’m doin wrong?

  83. David Stiller Says:

    Chris,

    My hunch has that you’re experiencing the phenomenon described here:

    (Perhaps) Unexpected Point of View: SWF Defers to HTML
    Unexpected “Gotcha” with Relative Paths in ActionScript

    Make sure to read both. :)

  84. Joe Says:

    I’m using Flash 8 Pro and am new to video. I’d like my video to stream and play and at the end of the stream, I would like to go to a labeled frame in my animation and proceed.

    I am doing that now with the timeline but that’s not working well.

    Any help would be appreciated.

  85. Joe Says:

    Just figured it out from your sample at top:

    stop();
    // Load the video
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(”videoname.flv”);
    // Monitor the video
    this.onEnterFrame = function() {
    if (ns.time >= 30) {
    gotoAndPlay(2);
    ;
    delete this.onEnterFrame;
    }
    }

    Thanks!

  86. David Stiller Says:

    Joe,

    Good for you! :) I was going to direct you among those earlier comments for starters. I’ll probably write an actual article on the topic before too long, but in the mean time, way to go!

  87. Anjan Says:

    hello ,

    is it possible to load .mpeg,.avi,.mp3 as we are loading .flv through net stream.

    and also i need the actionscript to find the format in perticular foder and then load in videoplayer. i mean can we write the code like if, else, condition to load differ formats which are avail in the folder.

    Advance thanks
    Anjan

  88. David Stiller Says:

    Anjan,

    Flash loads more file types than ever, including JPG, GIF, PNG, MP3, XML, CSS, TXT, and probably a few more — but the only video format it supports is FLV. That said, I see you did ask about MP3. The older Media Components support MP3, but they don’t use the NetStream class, as far as I know.

  89. Collin D Says:

    Hey David, I want to compliment you on how well written and helpfull your website is. I especially enjoyed the article on catching fruit flys - if you have any similiar techniques for women - please let me know.

    My flash problem is that I designed a flash website for my friend incorporating his video demos. I imported the files into .flv format, compressing them as much as I could.

    I then inserted the videos into movie symbols - so that the movie will play on the frame when accessed. Everything works fine on my computer. However online they do not load. The box does not even appear.

    - I checked the relative path article, however this doesn’t seem to be the problem in my case. The files are all located at

    [bunch of URLs]

    The “Loading” doesn’t actually work - I tried copy and pasting the code you had for the media.

    “stop();
    // Load the video
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(”PETERBdirectingREEL20072.flv”);
    // Monitor the video
    this.onEnterFrame = function() {
    if (ns.time >= 30) {
    gotoAndPlay(2);
    ;
    delete this.onEnterFrame;
    }
    }

    Unfortunately this only produces an error.

    I am sorry I am not more knowledgable - I just graduated with a B.A., can’t find a job - so am trying to do webpages in the meantime. I hope that one day I can be in your position to mentor people like myself.

    PS - this is the first USEFULL blog site I’ve come across - thanks again.

  90. David Stiller Says:

    Collin,

    I especially enjoyed the article on catching fruit flys - if you have any similiar techniques for women - please let me know.

    Haha! The latter doesn’t necessarily involve Saran Wrap with fork holes — at least, not in my experience. ;)

    I imported the [video] files into .flv format, compressing them as much as I could … then inserted the videos into movie symbols - so that the movie will play on the frame when accessed.

    This might actually be your problem, Collin. There’s nothing essentially wrong with embedding video content directly into a SWF’s movie clips, but doing so will definitely bulk up the SWF’s file size — and if you decide to go that route, then you don’t need the ActionScript from the above article; in fact, you wouldn’t need ActionScript at all.

    The ActionScript above — with NetConnection, NetStream, and so on — is something you can use when your video is external to the SWF. You don’t need the FLVPlayback Component or any other Component (I’m wondering if that’s what you meant by “the box”?) … the ActionScript handles the FLV loading by way of a Video object, as described way at the top. If by “the box” you mean the Video object, then that would make sense, because unless video is playing in it, a Video object can’t be seen.

    I think your best bet is to start with a fresh FLA file, free of anything but the bare essentials as described in the article. Make sure your Video object has the instance name videoPlayer. Somewhere, you must have missed a step, so retrace your path and see if you can find where one of the footprints turns left at Albuquerque.

  91. L Nguyen Says:

    Hi Guys,

    Why can’t we use a relative path to flv file in ns.play() command. It appears to me that flv files must be on the same level as SWF (e.g ns.play(”myvideo.flv”) works, but ns.play(”\myFlvFolder\myvideo.flv”) would not )

    Any thoughts would be appreciated.

    Lnguyen

  92. David Stiller Says:

    Lnguyen,

    Ah, you’ll be pleased to know that you can, indeed, use relative paths! :) In fact, if you think about it, the example in the original article shows a relative path — no path at all is a relative reference. The syntax of the path needs to be the same as for HTML, so flip those backslashes the other direction … /myFlvFolder/myvideo.flv.

    Check out the “Unexpected Point of View” and “Gotcha” articles for important information on relative paths in ActionScript.

  93. Denise Hambrick Says:

    I’m using Flash 8 to use flv videos and interation that will be delivered on CDs. I’m using the FLVPlayer component to successfully play the video, but have not been able to get the embedded event cues to work. Can you help?

    I tried using the code that was successful in MX2004, but it doesn’t work. Or I’m missing something. I’m in dire straights here. Please help.

    Thanks in advance. I love you blogs. Great information.

    Denise

  94. David Stiller Says:

    Denise,

    The FLVPlayback Component is new to Flash 8, so that may explain why your Flash MX 2004 code isn’t working. At its most basic, you’ll need something like this:

    var listener:Object = new Object();
    listener.cuePoint = function(evt:Object):Void {
      // instructions here
    };
    flvPlayer.addEventListener("cuePoint", listener);

    The variable listener is an arbitrarily named Object instance. A cuePoint property is created for this object and assigned to a function literal. This function acts on behalf of the FLVPlayback.cuePoint event and performs whatever you tell it to. Your FLVPlayback instance must “subscribe” the event to the listener, which occurs via the addEventListener() method. The function receives a parameter arbitrarily referenced as evt, and it’s that parameter that will contain the reference to your cue points via its own info property. So, for example, to trace cue point names …

    var listener:Object = new Object();
    listener.cuePoint = function(evt:Object):Void {
      trace(evt.info.name);
    };
    flvPlayer.addEventListener("cuePoint", listener);
  95. Pooch Says:

    Hi David,

    Great site, very helpful.

    Quick pointer for those experiencing the “stuttering” or choppy playback / choppy audio when using the FLVPlayback component. SET your bufferTime property to something higher than the default 0.1s I set mine to 5 and that solved the problem. Technically this is only supposed to matter when you’re streaming and not doing progressive download, but I garuntee it makes the difference!

    Now, the problem I’m having is a rather quirky one. I use the FLVPlayback component and feed the contentPath property dynamically using FlashVars. For some reason, on a few occasions when you load the video the seek bar scrubber, and the totalTime of the video fail to show up, but then if you reload it will appear again. The metadata is indeed properly encoded and as I said, it works sometimes (most times, but not others) It seems to me like the metadata info isn’t always loading, maybe the file playing before the metadata is populating the component is causing a problem? Anybody else run into this before?

    Thanks! Great site.

  96. David Stiller Says:

    Pooch,

    I haven’t personally run into the problem you describe, so I can only take a wild stab at a solution. If you look up the FLVPlayback class in the Components Language Reference, you’ll see that the class features an FLVPlayback.setSize() method. All v2 UI Components feature this method (if memory serves me), but the others inherit it from another class — FLVPlayback stands apart in that regard.

    Anyway, this method allows you to resize the Component via ActionScript, and it’s a very different sort of resizing than what would happen if you simply set the instance’s MovieClip._width and _height properties (yup, the MovieClip class also features in the mix, here). When Components resize, they initiate what I’ll call a “trickle down” effect, because it the resizing is passed along to all the sub-Components inside the main one, including buttons, sliders, and the like.

    So if you use ActionScript to set your FLVPlayback instance to its own current width and height, it might just shake loose the problem causing your occasional missing seekbar scrubber.

  97. Pooch Says:

    I’ll give that a go and let you know what I find.

    Thanks for the idea.

  98. Ravi Sharma Says:

    I am using Flash 2004 for player 7. I am using MediaPlayback controller for playing FLV file. But there is some problem with seek bar. When i am scrubbing the seekbar and then play it’s not working fine. PLZ suggest me the correct way to use MediaPlayBack controller.

  99. Alex Says:

    Hi guys,

    I see there is a ton of help here on movie components but I am lost with this part of the actionscript needed for my project. I have a MediaPlayback component loading a .flv from a URL, My flash movie consists of 2 frames, the first one is a picture when clicked it takes it to the second frame which contains the component with the video, I just want to find out how I can tell the component to play frame 1 again when the video is done within it.

    Thank you so much for your time,

    I appreciate it,

    Alex

  100. Senan Says:

    Hiya David - I’m working on an interactive trail map that utilizes an .flv file progressively downloaded in the first frame. I have set the actions in the first frame to:

    stop();

    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    FLVPlayback.attachVideo(ns);
    ns.play(”wachugoog4.flv”);

    this.onEnterFrame = function() {
    if (ns.time >= 20) {
    gotoAndPlay(2);
    ;
    delete this.onEnterFrame;
    }
    }

    due to the fact that the flv runs about 20 seconds; and at the very end, I’ve taken a screenshot of the final image, converted to movie, fade it out and transition to the actual trail map. Great in theory. It most often works, however, if you’re on a dial-up or simply have a weak signal, then your going to experience both choppiness AND it will go back to the first frame (beginning of .flv) which is of course, not what I want. How would you suggest preloading the .flv (without including playback controls) so it’s guaranteed to be fully loaded, runs through, fades out at end and is deleted.

    Here’s the link to the current app to show you what I’m up to — any suggestions are greatly appreciated!!

    http://www.northpoledesign.com/wachumap/wachu2.htm

    Thanks again!! I look forward to hearing from you!!

    Your site is bookmarked on my bookmark bar by the way :)

    Best -

    Senan

  101. David Stiller Says:

    To Alex …
    The goal of being alerted to the end of a video clip is a popular one in comments on this blog, as well as a number of forums I frequent. The way(s) it can be approached depend on the object used. In this case, you’re talking about one of the older Media Components, so your best bet is to fire up the Components Language Reference and look for events of the Media class. You’ll find a Media.complete event, which is dispatched when, according to the docs, “the playhead reaches the end of the media.” Unfortunately, this wording is a bit unclear. By “playhead,” in this case, they’re talking about the seek bar (the slider) on an instance of MediaPlayback or MediaController, rather than the timeline playhead. Make sure your Media Component instance has an instance name, than use that in place of the “myMedia” used in the sample code for that event.

    Given the popularity of this topic, I intend to write a blog entry that steps through an example both for the older Media Components as well as the newer FLVPlayback Component.

    To Senan …
    I suggest you invoke the NetStream.play() method, as you’ve done, then immediately invoke NetStream.pause(). Even though the video stream is paused, the FLV will continue to load. At this point, you may check the NetStream.bytesLoaded property against the NetStream.bytesTotal property in a loop to see when loading has completed. When that occurs, quit your loop and invoke NetStream.pause() a second time, which un-pauses the video.

  102. Senan Says:

    Edited to incorporate my reply …

    Senan,

    David - Thanks very much for the insights - wouldn’t have thought of pausing the NetStream. However, admittedly being an .flv newbie — could you take a look at the code below that I worked out from Flash Help? It seems to come up with an error while checking script.

    I’m glad to help. :) It’s important, though, that these comment entries don’t become the equivalent of a forum-style Q/A session — mainly because it would be too hard for me to keep up with them! If you see errors in your code, it’s often best to take a step back, then add one new line at a time, building up new expressions as you go. Add a bit, test; add a bit more, test.

    And after the video has completed playing - I want it to go to frame 2 & delete the .flv — do I need to leave in the onEnterFrame code?

    Dig through the previous comments — April, 2006, to Mark — and you’ll see a few suggestions on how to test for the end of the video when using the NetConnection/NetStream approach. MovieClip.onEnterFrame is an event that belongs to the MovieClip class, which means it can be assigned to the main timeline, which is what this refers to in this context. It’s really just a way to perform something repeatedly in ActionScript, which can be useful for checking loading values, for example. You could just as well use the setInterval() function. The decision on when to use it and when to stop it (with delete) depends on your needs. When you no longer need to perform an action continuously, it’s generally a good idea to stop.

    And do I need the stop action at the top of the script? Thanks soooo much David - I greatly appreciate your suggestions

    The stop() function works very similarly to how the MovieClip.stop() method does. If you need to stop the timeline in which this code resides, you’ll need one of those two. Functions can be thought of as “free range” methods, in that they aren’t associated with a class (such as MovieClip). Check out the entry for each in the ActionScript 2.0 Language Reference for complete details, but in this particular case, the function stands on its own, and the method requires an object reference, such as this.

    Here’s the code:

    stop();

    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    FLVPlayback.attachVideo(ns);
    ns.play(”wachugoog4.flv”);
    ns.pause(”wacchugoog.flv”);

    Careful! The NetStream.pause() method doesn’t accept any string parameters, such as the quoted name of your FLV file (which is different, by the way, from the one specified in the play() method). The only parameter it does accept is a Boolean. Here, too, the Components Language Reference is your best bet.

    I’m snipping the rest of your code, because you’re including a progress bar, which isn’t entirely necessary here. Could be useful — and I know the Help docs show it — but anything new like this should be ventured into with “baby steps.” Master one concept at a time.

    All you absolutely need so far is to play, then pause an FLV stream. Play a stream first, to make sure that works, then add the pause line to make sure your results change. Does the video still play? If so, something is wrong. Work out each step along the way. Does it pause? Good, next step: test the NetStream.bytesLoaded property.

    To do that, and to check for it repeatedly, you might use a MovieClip.onEnterFrame event. If so, then you may want to save your work, start a new FLA, and test just the enterFrame concept. Look up this event in the ActionScript 2.0 Language Reference, make sure you truly “get” what the event does — how to start it, how to stop it — before using it in conjunction with your existing video-related file.

    When you’ve mastered looping on a frame, bring that code into your video FLA and use it to trace the value of that NetStream instance’s bytesLoaded property.

    In this way, with small but sure steps, you’ll eventually get to what you’re after.

  103. David Stiller Says:

    To Ravi Sharma …
    Woops, didn’t mean to skip your question, Ravi. Unfortunately, I don’t think your comment gives me enough information to give you a solid answer. In any case, this particular blog entry has generated so many questions about the Media and FLVPlayback Components, I can see that a handful of articles on those topics would probably come in handy.

  104. Senan Says:

    David - I really want to thank you for your patient & generous insights. I can understand & appreciate the desire to maintain a non-forum-like use of this environment. You’re providing a valuable resource here!! Thanks again and I will be putting the bib back on and vow to ‘progressively’ learn to walk before I….Flash [video]. All the best.

    Senan

  105. David Stiller Says:

    Senan,

    You’re welcome, and thanks for the kind words! :)

  106. angurio Says:

    hi david. i am displaying several flvs on webpage at once - about 4. im using the custom UI FlvPlayback component at the moment, as it allows for easy skinning etc. here is the url:

    http://www.itsourmovie.com/?q=taxonomy/term/5

    However, problem is that each flv progressively downloads in the background, causing heavy bandwidth problems and the browser often just gets stuck and overloads. do you know of any way, that while still using progressive download, the video could just load the first frame - as a thumb, but *not* download in the background - i.e. just wait until the play button is pressed (obviously there would be buffer issues, but i can handle that). any play method would do, not just the flvplayback component. it seems very odd.. that adobe dont seem to provide a method to halt background downloading.. im being forced to look at Flash Media Server as a way of getting around the problem. Any thoughts appreciated.

  107. angurio Says:

    if i may continue - looking for example at the flvplayback api docs:

    LiveDocs link

    and this:

    http://www.adobe.com/devnet/flashcom/articles/flv_bestpractices.html

    ‘if video is over 1 minute, it should be streamed’. then it seems reasonable to conclude that adobe just dont really want to create progressive download tools in thier api.. as it will mean they cannot raise revenues by selling steaming software - i.e. FCS/FMS. there are plenty of ways that background downloading could be paused etc, or stopped to deal with bandwidth issues. the omission of such tools to me, seems *deliberate*.

    humbug

  108. David Stiller Says:

    angurio,

    it seems very odd.. that adobe dont seem to provide a method to halt background downloading.. im being forced to look at Flash Media Server as a way of getting around the problem. Any thoughts appreciated.

    It sounds like you’re frustrated — and that’s never fun, so I’m sorry to hear that — but on the bright side, you’re definitely not forced into purchasing Flash Media Server. True, it isn’t possible to natively halt background downloading in AS2, but if that’s the situation … work with what you’ve got. :) If you can’t stop such downloads, the answer may be to refrain from starting a download until you need it.

    The FLVPlayback class, documented in the Components Language Reference, serves as your entry point in hos this component works. You may have to consult the VideoPlayer class too, but ultimately, you’ll find that you can leave the contentPath property empty in the Component Inspector, then supply that value with ActionScript layer. It is possible to “hijack” the controller buttons and have the Play button perform a load as well as a play (rather than just a play).

    And you’re not stuck with FLVPlayback, either. ;) If you use a Video object, as described in the original article, you can use your own buttons to come up with a user interface that behaves exactly how you want.

    ‘if video is over 1 minute, it should be streamed’. then it seems reasonable to conclude that adobe just dont really want to create progressive download tools in thier api..

    I respectfully disagree. Flash has evolved by leaps and bounds with each new release. ActionScript 3.0 provides considerably more control of all aspects of a SWF, including video. I know in particular that it’s possible to halt a load for audio — again, in AS3 — which is an important improvement over the way AS2 handled audio files. I experimented with AS3 and video yet, but I have high hopes. Many of the quirks in Flash relate to legacy compatibility issues.

    there are plenty of ways that background downloading could be paused etc, or stopped to deal with bandwidth issues. the omission of such tools to me, seems *deliberate*.

    There are ways to halt a given download, I agree. One of the “tricks” in AS2 is to load a known, very small “dummy” file to “kill” the other download. There are workarounds, to be sure. But again, my first approach in this situation would be to leave the contentPath property blank — which means nothing loads in the background — then figure out a way to set that property via the Component’s skin buttons.

  109. angurio Says:

    thanks david, very much! that was a great help. perhaps i will update here when ive sorted it all out.. so i can help other souls in a similar position. :) thanks again.

  110. Paul Steven Says:

    I am using the Flash 8 FLVPlayback and my a custom UI Seekbar from the components panel. Problem is that the seekbar is not disappearing when I set its visibility to false

    mySeekBar._visible = false;

    It seems that once the SeekBar instance has been associated with the FLVPlayback instance, it will remain on screen.

    Basically my flash movie has 60 sections, some of which include a video. So I have the video components all set out on the stage and if there is no video for a particular section, I hide all the video components using the following code. I guess because I am only hiding and stopping the video, the seekbar still knows the video is lurking around. I do not know how else to get rid of the video. Possibly I can set the content path to empty but I got an error when I tried this.

    my_FLVPlybk._visible = false;
    my_FLVPlybk.stop();
    play_btn._visible = false;
    pause_btn._visible = false;
    mySeekBar._visible = false;

    Still, surely setting the visibility of the seekbar to false should hide it?

  111. Armando Says:

    Hi David, thanks for your helps.

    I’ve a problem.

    I’m making an Interactive CD. For it, I’m using an executable SWF file from which I’m loading other external SWF files.

    How can I put a FLV video in this case? I make ths question, because when I tried to put your code in a SWF file, the video doesn’t charge.

    I hope that you can understand me because I’m not english-speaker.

    Greetings,

    Armando Picón

  112. David Stiller Says:

    To Steven …

    The FLVPlayback Component — in fact, every Flash Component I can think of — extends the MovieClip class, so in theory, yes, you should be able to set the MovieClip._visible property of that seek bar to false. I haven’t studied the FLVPlayback class in exhaustive detail, so I can only make guesses at this point. It’s possible that, as you suspect, the class “knows” when video is present and when it isn’t, and overrides (or resets) the _visible property of that seek bar. I think your contentPath idea is a good one, but you said you got an error. What error was that?

    To Armando …
    I’ve run FLVs from a CD, so I know it’s possible. Don’t give up yet! :) My initial hunch is that you’re simply supplying an incorrect path to your FLV file. If you can get your project to run successfully from your hard drive, you should be able to move all your files and folders as they are to the CD, and everything should still work.

  113. Armando Says:

    Ok… I’m sure that “normally” works in a CD.

    But, Will it works from an executable SWF that load other SWF in an empty movie clip?? Because, I’m trying with many diffent ways for write the path… and not work :’(

  114. Armando Says:

    … and if the problem is about “security policies” ???

  115. David Stiller Says:

    Armando,

    I’m afraid I don’t know enough detail about your circumstances to answer with greater clarity. Flash Player security issues often get confusing, so I definitely recommend the official Adobe TechNotes there. At this point, you may want to do some trouble shooting — perhaps routing NetStream.onStatus messages to a dynamic text field so you can look for possible errors while the SWF runs inside the Projector.

  116. colin Says:

    hi david,
    fantastic blog…thanks so much for the info.

    i have used your code for setting up a video to play in a small banner. on my desktop it’s working perfectly. (i’ve also found a good way to solve the question of how to let the swf know when the video is done playing, which hopefully will be of service to you and your readers.)

    the problem i’m having is that when i post it on the web, the video is not playing right away. as you can see from the code the video is supposed to begin when the invisible button is clicked…however, the browser is displaying an error message, and then about 10-15 seconds later the video begins to play. (there is intentionally no audio in this video.)

    any thoughts as to why it would play correctly on the desktop but not on the web?

    thanks so much for any help,
    colin

    here’s the code i used:

    fscommand(”showmenu”, “false”);

    var my_nc:NetConnection = new NetConnection();
    my_nc.connect(null);
    var my_ns:NetStream = new NetStream(my_nc);
    videoBox.attachVideo(my_ns);
    my_ns.setBufferTime(35);
    my_ns.play(”gopher_small3.flv”);
    my_ns.seek(0);
    my_ns.pause();

    my_ns.onStatus = function(info) {
    if (info.code == “NetStream.Play.Stop”) {
    my_ns.seek(0);
    my_ns.pause();
    _root.gotoAndPlay(”complete”);
    }
    };

    btn_play.onRelease = function(){
    my_ns.play(”gopher_small3.flv”);
    mcPostit._visible = false;
    _root.gotoAndStop(”play”);
    };

    stop();

    here’s the file up on the web: (it should start when you click the yellow note…if you’re patient you’ll see it takes 10-15 seconds to begin rolling.)

    http://teakedit.com/testing/

  117. colin Says:

    hmmm,
    now it seems to be working pretty well…maybe because it was cached for me. could it have to do with the buffer time?

    (i tried to check that, which is why the code i sent you has a BufferTime of (35)…the actual version on the web has a BufferTime of (5).

    just trying to clarify,
    colin

  118. David Stiller Says:

    colin,

    By setting a buffer time of 35, you’re asking Flash to hold off actually playing the video until 35 seconds have been loaded. The default value for this property is 0.1 (one tenth of a second). So that almost certainly explains the delay. And you betcha, FLVs cache just like other files, so you’ll have to clear your cache each time in order to test afresh — or use a random number to “trick” Flash out of it.

    Thanks for the compliments!

  119. Arvid Says:

    Hi david, i have little experience in flash and i was wondering if you could tell me how to get the movie to repeat.

    Thank you.

  120. David Stiller Says:

    Arvid,

    By “movie,” most Flash developers mean a FLA or SWF, but because you posted to this FLV entry, my hunch is that you’re asking how to replay the FLV video file. If that’s it, simply invoke the NetStream.play() method again. Alternatively, you may invoke the NetStream.seek() method; in any case, the NetStream class is your best bet.

    Check it out in the ActionScript 2.0 Language Reference to see what your options are — properties are characteristics the object has, methods are things the object can do, and events are things the object can react to. You may want to read Objects: Your ActionScript Building Blocks for a general overview on classes, if these terms don’t feel especially comfortable yet.

  121. Kaspar Says:

    hey David,

    Great site, with awsome support!
    Ive made a php script which randomly pick a filename from a .xml file. Now i want the file to be played in my movie. I was thinking of something like this: - but I cant get it to work.

    sti = “http://www.mysite.com/kaspar/web/test.php?funktion=geturl”
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    videoPlayer.attachVideo(ns);
    ns.play(sti);

    My plan is to run the thing on a local network with php installed, which ‘method’ would you think could be the most appropriate ?
    FLVPlayback og videoPlayer or something different? videoclip size is about 10mb pr clip.

    Kaspar Hansen

  122. helena Says:

    hi

    i wonder how can i play flv with buttons? i have 3 videos and 3 buttons that will load the flv into a tv i created…i cant get the buttons to work

  123. David Stiller Says:

    To Kaspar …

    I have two thoughts. First, why not use Flash to load the XML itself? See the XML and XMLNode class entries in the ActionScript 2.0 Language Reference. It’s likely you could do the whole thing without PHP, though there’s nothing wrong with your PHP approach. My second thought — actually, this is an observation — is that you’ll need to load that PHP data via something like the LoadVars class. Your idea is entirely feasible, but that isn’t the way to set variables to external values in Flash.