WEB开发网
开发学院图形图像Flash FMS3系列(五):通过FMS实现时时视频聊天 阅读

FMS3系列(五):通过FMS实现时时视频聊天

 2009-05-05 12:06:31 来源:WEB开发网   
核心提示: privatefunctiononNetStatusHandler(evt:NetStatusEvent):void{trace(evt.info.code);if(evt.info.code=="NetConnection.Connect.Success"){ns=n

private function onNetStatusHandler(evt:NetStatusEvent):void
 {
       trace(evt.info.code);
       if(evt.info.code=="NetConnection.Connect.Success")
       {
           ns=new NetStream(nc);
           ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
           ns.client=new CustomClient();
           ns.attachCamera(cam);
           ns.attachAudio(mic);
           ns.publish(txtInput.text,"live");
       }
}

实现视频发布的核心技术点就是获取视频、音频数据,分别通过Camera和Microphone的静态方法实现。参考代码:

public function PublishStream():void
{
    btnPublish.label="发布视频";
    btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);
            
    //获取视频和声音,并将视频显示到Flash界面
    cam = Camera.getCamera();
    mic = Microphone.getMicrophone();
    video = new Video(320,240);
    video.attachCamera(cam);
    video.x=20;
    video.y=20;
    addChild(video);
}

通过以上步骤就完成了视频聊天的视频流发布端的开发,完整的示例代码如下:

package
{
    import flash.net.*;
    import flash.events.*;
    import flash.display.*;
    import flash.media.*;
    import fl.controls.*;
    
    public class PublishStream extends Sprite
    {
        private var video:Video;
        private var nc:NetConnection;
        private var ns:NetStream;
        private var cam:Camera;
        private var mic:Microphone;
        
        public function PublishStream():void
        {
            btnPublish.label="发布视频";
            btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);
            
            //获取视频和声音,并将视频显示到Flash界面
            cam = Camera.getCamera();
            mic = Microphone.getMicrophone();
            video = new Video(320,240);
            video.attachCamera(cam);
            video.x=20;
            video.y=20;
            addChild(video);
        }
        
        private function onPublishClick(evt:MouseEvent):void
        {
            nc = new NetConnection();
            nc.connect("rtmp://localhost/LiveStreams");
            nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
        }
        
        private function onNetStatusHandler(evt:NetStatusEvent):void
        {
            trace(evt.info.code);
            if(evt.info.code=="NetConnection.Connect.Success")
            {
                ns=new NetStream(nc);
                ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
                ns.client=new CustomClient();
                ns.attachCamera(cam);
                ns.attachAudio(mic);
                ns.publish(txtInput.text,"live");
            }
        }
    }
}

Tags:FMS 系列 通过

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接