ActionScript
Flash: Playing FLV/F4V Videos With Script
February 22, 2010
0

Playing F4V

F4V is the next incarnation of Flash Video which supports H.264 video (MPEG-4), which is more efficient for HD quality videos.   More about F4F: http://www.adobe.com/devnet/flv/.  To play F4V, the player must be Flash player 9

In many cases, the existing FLV player code can be used to play F4V.  The key is to republish the swf in Flash CS4 and to target Flash Player 9+.  Also, make sure that F4V file is properly associated with in the server: http://kb2.adobe.com/cps/402/kb402865.html.

Example

This example is written in Flash CS4 in order to demonstrate F4V playback, but the steps are the same in CS3 (just replace the video filename: example.f4v with a flv file.  Starting with a new Flash project, insert a Video object into the Library.  The option is in the Library window option menu.

az2

Name the symbol.

az3

Add it onto the stage, and name it.  In this example, I name it: video.

az4

Add the code below.  It’s a barebone code to play FLV, and not designed to be robust.  A related post on how to use Flash Component is here:

http://permadi.com/blog/2009/05/using-flvplayback-component-in-flash-cs3-professional/


var mNetConnection:NetConnection=new NetConnection();
mNetConnection.connect(null);

var mNetStream:NetStream=new NetStream(mNetConnection);
video.attachNetStream(mNetStream);
mNetStream.play("example.f4v");
mNetStream.client=this;
mNetStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStreamStatusEvent);

function onNetStreamStatusEvent(event:NetStatusEvent):void
{
  if (event.info.code == "NetStream.Play.Start")
  {
    trace("NetStream.Play.Start");
  }
  if (event.info.code == "NetStream.Play.StreamNotFound")
  {
    trace("Video Not Found");
  }
  if (event.info.code == "NetStream.Buffer.Full")
  {
  }
}

function onMetaData(metadata:Object):void
{
}

The line:

<pre>mNetStream.bufferTime=5;</pre>

is to indicate how many second of the video should be preloaded before playback start.

The line:

mNetStream.client=this;

is to indicate that this class will handle the onMetaData call (if you’re not doing anything with the metadate, the function can be left blank).  Without it, this error may appear:

Error #2044: Unhandled AsyncErrorEvent:.
text=Error #2095:
flash.net.NetStream was unable to invoke callback onMetaData.
error=ReferenceError:
Error #1069: Property onMetaData not found on flash.net.NetStream and there is no default value.

The example code can be downloaded here.  It is saved in Flash CS4.
Credit for the video used in the xample: The Internet Archive, item: Computer Chronicle 2000.  I trimmed the video to about 3.5 minutes for storage reason, watch the full 20+ minutes video at Computer Chronicle 2000

The encoder used is iSkysoft video converter trial version.