Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flash Developer Center /

Handling metadata and cue points in Flash video

by Peter deHaan

Peter deHaan

Content

  • Loading a video using the NetConnection and NetStream classes
  • Listening for the NetSteam object's asyncError event
  • Using the NetSteam class's client property to prevent the asyncError event
  • Creating a custom class and defining methods to handle the callback methods
  • Extending the NetStream class and adding methods to handle the callback methods
  • Extending the NetStream class and making it dynamic
  • Setting the NetStream object's client property to "this"

Modified

23 November 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Professional FLV | F4V metadata NetConnection NetStream video

Requirements

User level

Intermediate

Required products

  • Flash Professional (Download trial)

Sample files

  • example01.zip (14 KB)
  • example02.zip (14 KB)
  • example03.zip (27 KB)
  • example04.zip (14 KB)
  • example05.zip (15 KB)
  • example06.zip (28 KB)
  • example07.zip (27 KB)

This Quick Start article illustrates several techniques for handling the onMetaData and onCuePoint callback methods when using the NetConnection and NetStream classes to load Flash videos (FLVs). You'll discover how you can use the asyncError event (AsyncErrorEvent.ASYNC_ERROR) or client 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 asyncError and client 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 onMetaData and onCuePoint 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 onMetaData and onCuePoint event handlers, the asyncError is dispatched and informs you that the NetStream class was unable to invoke the callback onMetaData and onCuePoint:

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);

Result

The previous example loads a Flash video file and begins video playback. Once the video's metadata or cue points are encountered an asyncError 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:

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()

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 asyncError event.
  2. Set a value for the NetStream object's client 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 asyncError event dispatched by the NetStream object is to listen for the event using the addEventListener() method. This allows you to either handle or ignore the event.

Example

The following example loads an FLV and traces the asyncError event's text property whenever the asyncError is dispatched:

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); }

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:

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.

If you were looking at this SWF online in a web browser, no errors would be displayed since you are handling the asyncError 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 asyncError event is to use the client property of the NetStream class. The client property specifies the object on which callback methods are invoked. The default object is the NetStream object being created. If you set the client property to another object, callback methods will be invoked on that other object.

Example

The following example creates a NetStream object and sets its client property to an empty object, which prevents the asyncError from being dispatched:

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);

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

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; }

The previous example defines a handler method for the onMetaData 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:
var customClient:Object = {onMetaData:metaDataHandler};

This examples will display like this:

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

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 asyncError event dispatched by the NetStream object is to listen for the event using the addEventListener() method. This allows you to either handle or ignore the event.

Example

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

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);

The CustomClient class is as follows:

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

Note: The CustomClient class defines a handler for the onMetaData callback handler. If a cue point was encountered and the onCuePoint callback handler was called, an asyncError 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 onCuePoint callback method in your CustomClient class, or define an event handler for the asyncError 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 asyncError 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:

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);

The following code listing defines the CustomNetStream class that extends the NetStream class, and assigns the onMetaData and onCuePoint callback handler methods:

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"); } } }

If you want to rename the onMetaData and onCuePoint methods in the CustomNetStream class, you could use the following code:

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"); } }

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 onCuePoint and onMetaData 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:

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);

The DynamicCustomNetStream class is as follows:

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

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

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"); }

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 client property to this, Flash Player looks in the current scope for onMetaData() and onCuePoint() methods.

Example

The following code creates an instance of the NetStream class, and sets its client property to this:

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);

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

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

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.

Related Flash Quick Starts

  • Getting started with the ActionScript 3 FLVPlayback component
  • Using Adobe Media Encoder CS5
  • Handling metadata and cue points in Flash video

More Like This

  • Getting started with the ActionScript 3 FLVPlayback component
  • Using the Adobe Flash CS3 Video Encoder
  • Using Adobe Media Encoder CS4
  • Using Adobe Media Encoder CS5

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement