Listening for an FLV file’s metadata using the VideoDisplay control in Flex (Flex 2 edition)

The following example shows how you can use the internal MetadataEvent.METADATA constant to detect when the VideoDisplay has parsed the metadata from an FLV file in Flex 2.

Full code after the jump.

Method 1: use MetadataEvent.mx_internal::METADATA constant:

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="vertical"verticalAlign="middle"backgroundColor="white"><mx:Script><![CDATA[import mx.events.MetadataEvent;import mx.utils.ObjectUtil;import mx.core.mx_internal;private function onMetaData(evt:MetadataEvent):void {textArea.text = ObjectUtil.toString(evt.info);}private function videoDisplay_initialize():void {videoDisplay.addEventListener(MetadataEvent.mx_internal::METADATA, onMetaData);}]]></mx:Script><mx:VideoDisplay id="videoDisplay"source="http://www.helpexamples.com/flash/video/clouds.flv"initialize="videoDisplay_initialize();" /><mx:TextArea id="textArea"editable="false"width="100%"height="100%" /></mx:Application>

Method 2: use MetadataEvent.METADATA constant with use namespace mx_internal:

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="vertical"verticalAlign="middle"backgroundColor="white"><mx:Script><![CDATA[import mx.events.MetadataEvent;import mx.utils.ObjectUtil;import mx.core.mx_internal;use namespace mx_internal;private function onMetaData(evt:MetadataEvent):void {textArea.text = ObjectUtil.toString(evt.info);}private function videoDisplay_initialize():void {videoDisplay.addEventListener(MetadataEvent.METADATA, onMetaData);}]]></mx:Script><mx:VideoDisplay id="videoDisplay"source="http://www.helpexamples.com/flash/video/clouds.flv"initialize="videoDisplay_initialize();" /><mx:TextArea id="textArea"editable="false"width="100%"height="100%" /></mx:Application>

Method 3: use “metadataReceived” event name directly:

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="vertical"verticalAlign="middle"backgroundColor="white"><mx:Script><![CDATA[import mx.events.MetadataEvent;import mx.utils.ObjectUtil;private function onMetaData(evt:MetadataEvent):void {textArea.text = ObjectUtil.toString(evt.info);}private function videoDisplay_initialize():void {videoDisplay.addEventListener("metadataReceived", onMetaData);}]]></mx:Script><mx:VideoDisplay id="videoDisplay"source="http://www.helpexamples.com/flash/video/clouds.flv"initialize="videoDisplay_initialize();" /><mx:TextArea id="textArea"editable="false"width="100%"height="100%" /></mx:Application>

A big thanks to Arthur for the heads up, and Jamie for the suggestion.

For more information on the VideoDisplay control in Flex 2, see http://livedocs.adobe.com/flex/201/langref/mx/controls/VideoDisplay.html.

Any other suggestions? Leave them in the comments!