Tutorial – Playing FLV video in plain AS3 – Part 4 – Sample Usage

Hi Everyone..

This is the fourth (and most probably the last) installment of this series on playing FLV (and H.264 MP4) video in Flash from scratch.

Full source code for the library is here.

The related posts are:

For many of you, this might be the only post you are interested in. It will show you how to use the library.

Let’s start with the basics, or if you prefer, the essentials ;). Download the source which is here. Unzip it into your classpath, then create a new ActionScript class (I suggest FlashDevelop or for those who can deal with Eclipse (i can’t) Flex Builder). Comments are in the code.

List of Events
Now this is good, the video is playing, let’s look into the events generated by the FLVPlayer class. Their name is meant to be descriptive enough, and here is the full list:

  • MediaSizeEvent.SIZE — dispatched when the dimensions in pixels of the video is received
  • MediaEvent.STARTED_PLAYING — dispatched when the video starts playing
  • MediaEvent.FINISHED_PLAYING — dispatched when the video finished playing
  • MediaTimeEvent.DURATION — dispatched when the duration (seconds) of the video is received
  • MediaTimeEvent.TIME -- dispatched a few times a second to update the current position of the video
  • MediaEvent.ERROR — dispatched when an error is found while playing

Handling each event
First we need to update the initEvents function to add event listeners for each event…

MediaSizeEvent.SIZE
This event carries with it the dimensions in pixels of the FLV or MP4 file. It can be used to resize the Video object accordingly, either stretching to specific dimensions while maintaining aspect ratio, or fitting it exactly.

MediaEvent.STARTED_PLAYING & MediaEvent.FINISHED_PLAYING
These two events simply indicate when the media starts and finishes playing. I don’t know what you want to do then, so I simply trace it to the output.

MediaEvent.ERROR
Honestly, I don’t think I use this event yet, but maybe in the future. So again just trace it..

MediaTimeEvent.DURATION
This event contains the length in seconds of the media playing, in this case the FLV or MP4. This can be used to adjust your scrobbler bar.. for example, or to display in a textfield somewhere.

MediaTimeEvent.TIME
This event keeps you posted as to the progress of the position inside of the media. This can also be used to update your Scrobbler bar, or textfield.. It is dispatched 4 times per second, no matter how slowly or fast the media is playing.

Other useful functions
You can also explore certain functions, which are rather straightforward; such as:

  • pause
  • play
  • mute
  • setVolume
  • mute
  • seek
  • stop
  • close
  • reset

Cheers!

Martin

6 thoughts on “Tutorial – Playing FLV video in plain AS3 – Part 4 – Sample Usage”

  1. How does one implement a stop button in this program? When I try, the video disappears but the audio continues playing.

  2. If i int 2 videoPlayers and play them .

    _player.playMedia(media);
    _player1.playMedia(media1);

    The videos have a few seconds delay i would like them to start at the same time.

    Thanks.

  3. Puscasu, I suggest loading both videos (playMedia), and pause them right away so they don’t start, then catch the media_ready event (I forget the exact name, I coded that thing 3 years ago!) and then start both of them at the same time. The delay is cause by Flash, not my code. Loading videos is resource intensive…

  4. Hello,
    I am new to flash and AS.
    I am using your source code to play the FLV. I am using camera before starting FLV video.
    My problem is that the camera view coming over the FLV Video which palying in background.
    I need the FLV on over the camera view so that transparent FLV video will come over the camera view.
    How can i do this ?

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">