Tutorial – Playing FLV video in plain AS3 – Part 1

11 actionscript,as3,as3 tutorials,flash,work  |   June 3, 2008  |     10797

Hi Everyone,

today I will explain how to play .FLV files that are hosted on a standard HTTP server, no fancy streaming. You can use pre-made components to do so, but sometimes, for whatever reason, you want to do it yourself. I wrote my first FLV playing algorithm in AS3 about 10 months ago, it has evolved since and here is the breakdown on how I make it work. It’s been used in many widgets, mostly for Music Nation.

Objects we will play with

There are a number of objects available in the standard AS3 library that is included with both Flash CS3 and Flex. Here is a list of those we will be using, along with a point form explanation of what we will use them for

  • flash.net.NetConnection — to establish the connection to the HTTP server
  • flash.net.NetStream — to “stream” or download the media from the HTTP server
  • flash.media.SoundTransform — to modify the volume at which the FLV is playing
  • flash.media.Video — this holds the visual representation of the video

This being said, we will also have to catch quite a few potential errors, here is the list of error events we will deal with

  • flash.events.SecurityErrorEvent — on NetConnection
  • flash.events.IOErrorEvent — on NetConnection AND NetStream
  • flash.events.AsyncErrorEvent — on NetStream

And finally we will use the flash.net.NetStatusEvent on both the NetStream and the NetConnection to keep track of what is happening.

First bit of code..

I will start with the basics..

That’s it. With this code, you’ll be playing a FLV file on the Stage. No controls yet…

The MetaData

This line of code

enables the object (“this” in this case) to receive special information about the playing Stream. You need to implement functions to catch that information, namely: onMetaData, onCuePoints, onPlayStatus. In the case of playing .FLV’s off an HTTP server, the onCuePoints and onPlayStatus are completely useless.

There are a number of things that are important about an FLV file that is playing, which are both received when the FLV starts loading:

  • video size in pixels
  • actual duration of the FLV in seconds

Here is how you catch it:

Handling Events & Errors

Both the NetStream and NetConnection objects throw errors.

ok that’s it for today, I will write part 2 tomorrow or the next day. In part two I will cover the trickier stuff, tracking the loading progress, enabling play/pause/stop, canceling the loading process of a FLV, seeking to a certain position, tracking the position of the stream and finally knowing exactly when the FLV is done playing!

Cheers!

Comments
  • October 1, 2008 at 2:45 pm

    Giannis T.

    Thank you for a really enlightening articles series!

  • August 11, 2009 at 4:12 pm

    Jack

    Hey,

    Thank you so much for putting these tutorials online. You are an invaluable help to peoples understanding of ActionScript.

    I followed the tutorial and everything was working fine until I set up the Security/Error/NetStatus functions. It then stated that an instance was null and would not compile the file. I built a class for this and attached it to the Fla through the Document class. I imagine that in the next 2 tutorials it will clear up.

    Thanks for all your incredible help. I in turn, will set up the same kind of blog when I get to your level to help people out like you helped me.

    Why would it do that?

  • October 29, 2009 at 2:13 am

    Raman

    This tutorial is excellent .

    I want to create flv player with all the functionalities but
    want that playing video to be manged by user.
    i.e suppose if our video is 2 min long and user wants to view a part of video from timings 1min to 1min 30 sec ..
    how can we do that

  • January 12, 2010 at 3:07 am

    Mark

    Nice tutorial, but where do you call the onMetaData function? I don’t get the trace of the function in my output? I’ve tried to use a client object, but that didn’t work.

    //

    _client = new Object();

    _stream = new NetStream(_connection);
    _stream.client = _client;

    _client.onMetaData = onMetaData;

    //

    Thank you for responding!
    Mark

  • January 14, 2010 at 2:44 pm

    dbam

    I’m really thankful for this collection on FLV playback error-handling.
    Extremely HAND(L)Y!

    Have a good day.

  • January 17, 2010 at 9:07 pm

    Rowan Gray

    I’ve just nabbed your code because my own FLV player is aweful, I did intend to recode it, but your player here is so beautifully coded I’m just going to use that and
    add on bits if I need extra functions.

    Thank you.

  • January 17, 2010 at 11:14 pm

    Rowan Gray

    should add:

    public function onXMPData(infoObject:Object):void { }

    to FLVPlayer, to silence the onXMPData error 😉

  • April 20, 2010 at 3:42 am

    charles

    good tutorial.
    i am looking forward to get my hands on the next part.

  • June 10, 2010 at 7:27 am

    Germo

    Thanks this tutorial, much easy to use than origin description in help.

  • May 13, 2015 at 4:01 am

    Aisyah

    the application of mine created by Flash and its had a video in it. But it wont play on mobile phone after i publish it to .apk format. how do solve the problem? is there any method or even as3?

  • May 13, 2015 at 5:21 am

    mlegris

    unfortunately I stopped doing flash about 4 years ago, before publishing to mobile was possilbe. I can’t help but maybe use stack-exachange?

Leave a Reply