Play local Video(flv mainly) in ActionScript3.0


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 used
var _duration:Number;

// create video object to display the video content
// and add it to display list
var _video:Video = new Video();
addChild(_video);

// create NetConnection object, connect it to null, means 
// no server designated
var _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 stream
var _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 listener
function metaDataListener(metaData:Object):void
{
	_duration = metaData.duration;
	_video.width = metaData.width;
	_video.height = metaData.height;
}

// handler function for net connection status
function onStatus(__e:NetStatusEvent):void
{
	trace(__e.info.code);
	if("NetStream.Play.Stop" == __e.info.code)
	{
		_stream.play("stand-by-me.flv");
	}
}

// error handlers
function 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.Success
NetStream.Play.Start
NetStream.Buffer.Full
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Empty
NetStream.Buffer.Flush
NetStream.Play.Stop
NetStream.Play.Start
NetStream.Buffer.Full
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Flush
NetStream.Buffer.Empty
NetStream.Buffer.Flush
NetStream.Play.Stop

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


fla link


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值