注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Handling metadata and cue points in Flash video(AsyncErrorEvent text=Error #2095 flash.net.NetStream)  

2011-04-13 00:35:59|  分类: 资料 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

From: Handling metadata and cue points in Flash video


This Quick Start article illustrates several techniques for handling the DE<onMetaDataDE< and DE<onCuePointDE< callback methods when using the NetConnection and NetStream classes to load Flash videos (FLVs). You'll discover how you can use the DE<asyncErrorDE< event (DE<AsyncErrorEvent.ASYNC_ERRORDE<) or DE<clientDE< property in the NetStream class to handle or ignore the meta data or cue points.

The next sections describe the most popular and useful ways to use the DE<asyncErrorDE< and DE<clientDE< property when working with loading videos.

Loading a video using the NetConnection and NetStream classes

There are many cases where you may prefer to create your own custom video player to play videos instead of using an existing component. For example, you may be trying to build a certain feature into your custom video player, or perhaps you're trying to make a video player with a very small file size. When building your own code, it is important to understand how the DE<onMetaDataDE< and DE<onCuePointDE< event handlers are used so you can handle them accordingly.

Example

The following example creates a new NetConnection, NetStream, and Video object to dynamically load an FLV file and display it in an SWF file. Since the specified FLV contains metadata and three cue points and there are no handlers defined for the DE<onMetaDataDE< and DE<onCuePointDE< event handlers, the DE<asyncErrorDE< is dispatched and informs you that the NetStream class was unable to invoke the callback DE<onMetaDataDE< and DE<onCuePointDE<:

 DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var myVideo:Video = new Video(); myVideo.attachNetStream(ns); addChild(myVideo); DE<

Result

The previous example loads a Flash video file and begins video playback. Once the video's metadata or cue points are encountered an DE<asyncErrorDE< is dispatched.

If you're running this code in the Flash authoring environment, you'll see that four errors are displayed in the Output panel with error messages similar to the following:

DE<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. at asyncErrorExample_fla::MainTimeline/asyncErrorExample_fla::frame1() Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. error=ReferenceError: Error #1069: Property onCuePoint not found on flash.net.NetStream and there is no default value. at asyncErrorExample_fla::MainTimeline/asyncErrorExample_fla::frame1() Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. error=ReferenceError: Error #1069: Property onCuePoint not found on flash.net.NetStream and there is no default value. at asyncErrorExample_fla::MainTimeline/asyncErrorExample_fla::frame1() Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. error=ReferenceError: Error #1069: Property onCuePoint not found on flash.net.NetStream and there is no default value. at asyncErrorExample_fla::MainTimeline/asyncErrorExample_fla::frame1() DE<

If you were looking at this SWF online in a web browser, you'd see the following errors instead:

Unhandled AsyncErrorEvent in debug version of Adobe Flash Player 9

To prevent these errors, you have two primary solutions:

  1. Add an event listener for the DE<asyncErrorDE< event.
  2. Set a value for the NetStream object's DE<clientDE< property.

To get the source files for this example, including Flash Professional CS5 versions of the files, download example01.zip at the top of this page.

Listening for the NetSteam object's asyncError event

One of the easiest ways to handle the DE<asyncErrorDE< event dispatched by the NetStream object is to listen for the event using the DE<addEventListener()DE< method. This allows you to either handle or ignore the event.

Example

The following example loads an FLV and traces the DE<asyncErrorDE< event's DE<textDE< property whenever the DE<asyncErrorDE< is dispatched:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var myVideo:Video = new Video(); myVideo.attachNetStream(ns); addChild(myVideo); function asyncErrorHandler(event:AsyncErrorEvent):void { trace(event.text); } DE<

Result

If you're running this code in the Flash authoring environment, you'll see that the Output panel now displays the text similar to the following:

DE<Error #2095: flash.net.NetStream was unable to invoke callback onMetaData. Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. Error #2095: flash.net.NetStream was unable to invoke callback onCuePoint. DE<

If you were looking at this SWF online in a web browser, no errors would be displayed since you are handling the DE<asyncErrorDE< event.

To get the source files for this example, including Flash Professional CS5 versions of the files, download example02.zip at the top of this page.

Using the NetSteam class's client property to prevent the asyncError event

The second way to handle the DE<asyncErrorDE< event is to use the DE<clientDE< property of the NetStream class. The DE<clientDE< property specifies the object on which callback methods are invoked. The default object is the NetStream object being created. If you set the DE<clientDE< property to another object, callback methods will be invoked on that other object.

Example

The following example creates a NetStream object and sets its DE<clientDE< property to an empty object, which prevents the DE<asyncErrorDE< from being dispatched:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.client = new Object(); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var myVideo:Video = new Video(); myVideo.attachNetStream(ns); addChild(myVideo); DE<

The above code prevents the DE<asyncErrorDE< from being dispatched, but it also ignores the DE<onMetaDataDE< and DE<onCuePointDE< event callbacks. If you wanted to define handlers for these two callbacks, you could use the following code which predefines a custom client object:

DE<var customClient:Object = new Object(); customClient.onMetaData = metaDataHandler; var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.client = customClient; ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var myVideo:Video = new Video(); myVideo.attachNetStream(ns); addChild(myVideo); function metaDataHandler(infoObject:Object):void { myVideo.width = infoObject.width; myVideo.height = infoObject.height; } DE<

The previous example defines a handler method for the DE<onMetaDataDE< handler which resizes the Video object on the Stage to match the dimensions of the FLV document, as reported by the metadata.

Tip: You can also use the following shorthand notation for creating an object and specifying a handler:
DE<var customClient:Object = {onMetaData:metaDataHandler};DE<

This examples will display like this:

To get the source files for this example, including Flash Professional CS5 versions of the files, download example03.zip at the top of this page.

Note: All of the following examples will display just like the example shown above. However some of the following examples will show additional trace information in the Flash concole window when they are run within the Flash CS3 authoring tool.

Creating a custom class and defining methods to handle the callback methods

One of the easiest ways to handle the DE<asyncErrorDE< event dispatched by the NetStream object is to listen for the event using the DE<addEventListener()DE< method. This allows you to either handle or ignore the event.

Example

The following example sets the NetStream object's DE<clientDE< property to a custom class, CustomClient, which defines handlers for the callback methods:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.client = new CustomClient(); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var vid:Video = new Video(); vid.attachNetStream(ns); addChild(vid); DE<

The CustomClient class is as follows:

DE<package { public class CustomClient { public function onMetaData(infoObject:Object):void { trace("metadata"); } } } DE<

Note: The CustomClient class defines a handler for the DE<onMetaDataDE< callback handler. If a cue point was encountered and the DE<onCuePointDE< callback handler was called, an DE<asyncErrorDE< event would be dispatched saying "flash.net.NetStream was unable to invoke callback onCuePoint." To prevent this error, you would either need to define an DE<onCuePointDE< callback method in your CustomClient class, or define an event handler for the DE<asyncErrorDE< event.

To get the source files for this example, including Flash Professional CS5 versions of the files, download example04.zip at the top of this page.

Extending the NetStream class and adding methods to handle the callback methods

The second way to handle the DE<asyncErrorDE< event is to use the client property of the NetStream class.

Example

The following code creates an instance of the CustomNetStream class, which is defined in a later code listing:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:CustomNetStream = new CustomNetStream(nc); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var vid:Video = new Video(); vid.attachNetStream(ns); addChild(vid); DE<

The following code listing defines the CustomNetStream class that extends the NetStream class, and assigns the DE<onMetaDataDE< and DE<onCuePointDE< callback handler methods:

DE<package { import flash.net.NetConnection; import flash.net.NetStream; public class CustomNetStream extends NetStream { public function CustomNetStream(nc:NetConnection) { super(nc); } public function onMetaData(infoObject:Object):void { trace("metadata"); } public function onCuePoint(infoObject:Object):void { trace("cue point"); } } } DE<

If you want to rename the DE<onMetaDataDE< and DE<onCuePointDE< methods in the CustomNetStream class, you could use the following code:

DE<package { import flash.net.NetConnection; import flash.net.NetStream; public class CustomNetStream extends NetStream { public var onMetaData:Function; public var onCuePoint:Function; public function CustomNetStream(nc:NetConnection) { onMetaData = metaDataHandler; onCuePoint = cuePointHandler; super(nc); } private function metaDataHandler(infoObject:Object):void { trace("metadata"); } private function cuePointHandler(infoObject:Object):void { trace("cue point"); } } DE<

To get the source files for this example, including Flash Professional CS5 versions of the files, download example05.zip at the top of this page.

Extending the NetStream class and making it dynamic

You can extend the NetStream class and make the subclass dynamic so that DE<onCuePointDE< and DE<onMetaDataDE< callback handlers can be added dynamically.

Example

The following code creates an instance of the dynamic DynamicCustomNetStream class, which is defined in a later code listing:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:DynamicCustomNetStream = new DynamicCustomNetStream(nc); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var vid:Video = new Video(); vid.attachNetStream(ns); addChild(vid); DE<

The DynamicCustomNetStream class is as follows:

DE<package { import flash.net.NetConnection; import flash.net.NetStream; public dynamic class DynamicCustomNetStream extends NetStream { public function DynamicCustomNetStream(nc:NetConnection) { super(nc); } } } DE<

Even with no handlers for the DE<onMetaDataDE< and DE<onCuePointDE< callback handlers, no errors are thrown since the DynamicCustomNetStream class is dynamic. If you want to define methods for the DE<onMetaDataDE< and DE<onCuePointDE< callback handlers, you could use the following code:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:DynamicCustomNetStream = new DynamicCustomNetStream(nc); ns.onMetaData = metaDataHandler; ns.onCuePoint = cuePointHandler; ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var vid:Video = new Video(); vid.attachNetStream(ns); addChild(vid); function metaDataHandler(infoObject:Object):void { trace("metadata"); } function cuePointHandler(infoObject:Object):void { trace("cue point"); } DE<

To get the source files for this example, including Flash Professional CS5 versions of the files, download example06.zip at the top of this page.

Setting the NetStream object's client property to "this"

By setting the DE<clientDE< property to DE<thisDE<, Flash Player looks in the current scope for DE<onMetaData()DE< and DE<onCuePoint()DE< methods.

Example

The following code creates an instance of the NetStream class, and sets its DE<clientDE< property to DE<thisDE<:

DE<var nc:NetConnection = new NetConnection(); nc.connect(null); var ns:NetStream = new NetStream(nc); ns.client = this; ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); var vid:Video = new Video(); vid.attachNetStream(ns); addChild(vid); DE<

If the DE<onMetaDataDE< or DE<onCuePointDE< callback handlers are called and no methods exist to handle the callback, no errors are generated. To handle these callback handlers, create an DE<onMetaData()DE< and DE<onCuePoint()DE< method in your code, as seen in the following snippet:

DE<function onMetaData(infoObject:Object):void { trace("metadata"); } function onCuePoint(infoObject:Object):void { trace("cue point"); }DE<

To get the source files for this example, including Flash Professional CS5 versions of the files, download example07.zip at the top of this page.


For more information

For more information about this topic, see "Using the Adobe Flash CS3 Video Encoder".

For more information about this topic, see the NetStream class in the ActionScript 3.0 Reference for the Adobe Flash Platform.

  评论这张
 
阅读(1052)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018