監視 NetStream 事件

兩種事件提供有用的使用資料: netStatus mediaTypeData 。此外,計時器還可用來定期記錄 NetStream 播放磁頭的位置。

netStatus 事件提供資訊,可讓您用來判斷使用者已檢視的串流量。緩衝區和 RTMFP 串流轉換事件也會產生 netStatus 事件。

mediaTypeData 事件提供中繼和 XMP 資料資訊。Netstream.Play.Complete 事件會當做 mediaTypeData 事件來傳送。您也可以透過 mediaTypeData 事件使用串流所內嵌的其他資料,包括提示點、文字和影像。

下列範例說明如何在應用程式中,建立類別來監視任何作用中 NetStreams 的狀態和資料事件。一般而言,這種類別會將有意用來分析的資料上傳到伺服器,以進行收集。

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