Adobe AIR: un semplice player per lo streaming live di Flash Media Server
Avevo l’esigenza di testare dei canali streaming pubblicati da un FMS ed ho implementato questo semplice client con funzionalità di debug di cui allego il codice.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”{init()}” width=”560″ height=”474″ borderColor=”#1A41C0″ backgroundGradientAlphas=”[1.0, 1.0]” backgroundGradientColors=”[#123BB3, #03097F]“>
<mx:Script>
<![CDATA[
import mx.messaging.channels.AMFChannel;
import mx.utils.ObjectUtil;
private var meta:Object;
public var nc : NetConnection ;
public var ns : NetStream ;
public var video: Video = new Video;
private function init () : void {
nc = new NetConnection ()
nc.objectEncoding = ObjectEncoding.AMF3 ;
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus ) ;
nc.addEventListener( AsyncErrorEvent.ASYNC_ERROR, onAsyncError ) ;
nc.addEventListener(IOErrorEvent.IO_ERROR, onIOError ) ;
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError ) ;
nc.connect( "rtmp://www.serverflash.xxx/live/"); // url del server FMS
}
private function onNetStatus ( e : NetStatusEvent ) : void {
var nsClient:Object = {};
nsClient.onMetaData = ns_onMetaData;
nsClient.onCuePoint = ns_onCuePoint;
trace ( e.info.code ) ;
//-- if is connected
if ( e.info.code.indexOf("Success") > -1 ){
ns = new NetStream (nc ) ;
ns.addEventListener(NetStatusEvent.NET_STATUS, onNsStatus )
ns.client = nsClient;
ns.play("myStream"); // nome dello Stream
video = new Video();
video.attachNetStream(ns);
//panelVideo.addChild(video);
uic.addChild(video);
}
}
private function onAsyncError ( e : AsyncErrorEvent ) : void {
trace (e.text ) ;
}
private function onIOError ( e : IOErrorEvent ) : void {
trace ( e.text ) ;
}
private function onSecError ( e : SecurityErrorEvent ) : void {
trace ( e.text ) ;
}
private function onNsStatus ( e : NetStatusEvent ) : void {
trace ( "netstatus------>" + e.info.code ) ;
}
private function ns_onMetaData(item:Object):void {
trace("meta");
meta = item;
video.width = item.width;
video.height = item.height;
//panel.title = "framerate: " + item.framerate;
panel.visible = true;
trace(ObjectUtil.toString(item));
}
private function ns_onCuePoint(item:Object):void {
trace("cue");
}
]]>
</mx:Script>
<mx:Panel id=”panel” visible=”true” horizontalAlign=”center” title=”Video Stream” left=”80″ right=”80″ bottom=”58″ top=”46″>
<mx:UIComponent id=”uic” width=”345″ height=”246″/>
<mx:ControlBar>
<mx:Button label=”Play/Pause” click=”ns.togglePause();” />
<mx:Button label=”Rewind” click=”ns.seek(0); ns.pause();” enabled=”false”/>
</mx:ControlBar>
</mx:Panel>
<mx:Label x=”435″ y=”10″ text=”InfoDesk” color=”#DEF3F7″ fontWeight=”bold” fontSize=”18″/>
</mx:WindowedApplication>
