监控 NetStream 事件

有两类事件可提供有价值的使用数据: netStatus mediaTypeData 。 此外,可使用定时器来定期记录 NetStream 播放头的位置。

netStatus 事件提供您可用于确定用户观看了多少流的信息。缓冲区和 RTMFP 流转换事件也会导致 netStatus 事件。

mediaTypeData 事件可提供元数据和 XMP 数据信息。Netstream.Play.Complete 事件作为 mediaTypeData 事件调度。嵌套在流中的其他数据也可通过 mediaTypeData 事件获取,这些数据包括提示点、文本和图像。

下面的示例展示了如何创建一个类,这个类可以监视应用程序中任意活动 NetStream 的状态和数据事件。通常,该类会将需要分析的数据上传到服务器进行收集。

package com.adobe.example 
{ 
    import flash.events.NetDataEvent; 
    import flash.events.NetMonitorEvent; 
    import flash.events.NetStatusEvent; 
    import flash.net.NetMonitor; 
    import flash.net.NetStream; 
     
    public class NetStreamEventMonitor 
    { 
        private var netmon:NetMonitor; 
        private var heartbeat:Timer = new Timer( 5000 ); 
         
        public function NetStreamEventMonitor() 
        { 
            //Create NetMonitor object 
            netmon = new NetMonitor(); 
            netmon.addEventListener( NetMonitorEvent.NET_STREAM_CREATE, newNetStream ); 
 
            //Start the heartbeat timer 
            heartbeat.addEventListener( TimerEvent.TIMER, onHeartbeat ); 
            heartbeat.start(); 
        } 
         
        //On new NetStream 
        private function newNetStream( event:NetMonitorEvent ):void 
        { 
            trace( "New Netstream object"); 
            var stream:NetStream = event.netStream; 
            stream.addEventListener(NetDataEvent.MEDIA_TYPE_DATA, onStreamData); 
            stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);             
        } 
         
        //On data events from a NetStream object 
        private function onStreamData( event:NetDataEvent ):void 
        { 
             
            var netStream:NetStream = event.target as NetStream; 
            trace( "Data event from " + netStream.info.uri + " at " + event.timestamp ); 
            switch( event.info.handler ) 
            { 
                case "onMetaData": 
                    //handle metadata; 
                    break; 
                case "onXMPData": 
                    //handle XMP; 
                    break; 
                case "onPlayStatus": 
                    //handle NetStream.Play.Complete 
                case "onImageData": 
                    //handle image 
                    break; 
                case "onTextData": 
                    //handle text 
                    break; 
                default: 
                    //handle other events 
                     
            } 
        } 
         
        //On status events from a NetStream object 
        private function onStatus( event:NetStatusEvent ):void 
        { 
            trace( "Status event from " + event.target.info.uri + " at " + event.target.time ); 
            //handle status events 
        } 
        //On heartbeat timer 
        private function onHeartbeat( event:TimerEvent ):void 
        { 
            var streams:Vector.<NetStream> = netmon.listStreams(); 
            for( var i:int = 0; i < streams.length; i++ ) 
            { 
                trace( "Heartbeat on " + streams[i].info.uri + " at " + streams[i].time ); 
                //handle heartbeat event 
            } 
        } 
         
    } 
}