Play local Video(flv mainly) in ActionScript3.0

来源:互联网 发布:淘宝权全民直播 编辑:程序博客网 时间:2024/06/10 03:55


Before going in-depth and develop complex application, we shuold clearify the basic mechanism of how Flash Player tackle with video files. It provide some facilities to access the media data through network, they are a serial of classes. The main ones are Video, NetStream and NetConnection. A picture is always worth sound words, following is a diagram depicts the relationship among some concepts and the correspending classes:


stream-video


Not as implied as there names, Video class doesn't handle the manipulation of video indeed, instead, its responsiblity is just to display the content, it can be used to display different kind of video content, the source varies, may be a live stream from a camera, but we only explore the case that the video is a file stored on local hard disk. And NetStream is the one that used to control the video, such as playing, pausing, seeking and stop. It would be attached to a Video object, and it must relay on NetConnection object, to work.


And now we draw a rough sketch of code to demostrate how those classes work:


/* An experiment that to explore   * how to play local video * in actionscript 3.0 * Created by Vincent Zhang, 11-July-2011 */import flash.media.Video;import flash.net.NetConnection;import flash.net.NetStream;import flash.events.NetStatusEvent;import flash.events.SecurityErrorEvent;import flash.events.IOErrorEvent;import flash.events.AsyncErrorEvent;// variable for storing duration of the video file,// may or may not be usedvar _duration:Number;// create video object to display the video content// and add it to display listvar _video:Video = new Video();addChild(_video);// create NetConnection object, connect it to null, means // no server designatedvar _connection:NetConnection = new NetConnection();_connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);_connection.addEventListener(IOErrorEvent.IO_ERROR, onIOError);_connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);_connection.connect(null);// create NetStream object with the NetConnection object,// attach it to Video object// then it can be use to play and control the video streamvar _stream:NetStream = new NetStream(_connection);_stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);_stream.addEventListener(IOErrorEvent.IO_ERROR, onIOError);_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);_video.attachNetStream(_stream);// play the specific flv file// this function receive a string as parameter// to specify the url of the video file_stream.play("PICHONKUN.flv");// object play a role as listener// it is for fetching the meta data of the video file// such as W & H, duration and etc...var _clientObject:Object = new Object();_clientObject.onMetaData = metaDataListener;_stream.client = _clientObject;// handler function for listenerfunction metaDataListener(metaData:Object):void{_duration = metaData.duration;_video.width = metaData.width;_video.height = metaData.height;}// handler function for net connection statusfunction onStatus(__e:NetStatusEvent):void{trace(__e.info.code);if("NetStream.Play.Stop" == __e.info.code){_stream.play("stand-by-me.flv");}}// error handlersfunction onIOError(__e:IOErrorEvent):void{trace("IOErrorHandler: " + __e);}function onSecurityError(__e:SecurityErrorEvent):void{trace("securityErrorHandler: " + __e);}function onAsyncError(__e:AsyncErrorEvent):void{// ignore AsyncErrorEvent events.}

Since the video file resides locally, there is no server invlved, so the parameter passed to netstream.play() is null.

The statement

trace(__e.info.code);

 inside onStatus() will output the followings:


NetConnection.Connect.SuccessNetStream.Play.StartNetStream.Buffer.FullNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.EmptyNetStream.Buffer.FlushNetStream.Play.StopNetStream.Play.StartNetStream.Buffer.FullNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.FlushNetStream.Buffer.EmptyNetStream.Buffer.FlushNetStream.Play.Stop

Finally, this sample program will go into a loop of playing stand-by-me.flv again and again.


fla link


原创粉丝点击