0

又是我!我尝试使用 actionscript 开发我的聊天!我取得了一些成功!这是我的代码:

动作脚本:

<?xml version="1.0" encoding="utf-8"?>

        private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/850ecff774ce98e0c0c765f3-29ce3314ba14/";
        private var nc:NetConnection;
        private var ss:NetStream; 
        private var rs:NetStream;
        private var myPeerID:String; 
        private var recvStreams:Object = new Object();
        private var sendStreams:Object = new Object();
        private var recvVideos:ArrayCollection;
        private var myVideo:Video;
        private var microphone:Microphone;
        //private var uics:ArrayCollection;

        private function init():void {
            Security.allowDomain("*");
            myVideo=getVideoBox();
            ExternalInterface.addCallback("initConnection",initConnection);
            ExternalInterface.addCallback("sendSomeData",sendSomeData);
            ExternalInterface.addCallback("initReceiveStream",initReceiveStream);
            ExternalInterface.call("p2pStartInit");
        }

        private function getVideoBox(width:int=320,height:int=240,x:int=0,y:int=0):Video{
            var video:Video = new Video(width,height);
            video.x = x == 0 ? -width/2 : x;
            video.y = y == 0 ? -height/2 : y;
            var uic:UIComponent = new UIComponent();
            uic.addChild(video);
            addElement(uic);
            return video;
        }

        private function initConnection():void{
            nc = new NetConnection();
            nc.maxPeerConnections = 1000;
            nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
            nc.connect(SERVER_ADDRESS);
        }

        private function setVolume(s:NetStream,level:Number=0.8):NetStream{
            s.soundTransform = new SoundTransform(level);
            return s;
        }

        private function setMicrophoneLevel(level:Number = 0.8):void{
            microphone.soundTransform = new SoundTransform(level);
        }

        private function setupCamera(width:int=320,height:int=240):void{
            var cam:Camera = Camera.getCamera();
            cam.setMode(width, height, 25);
            cam.addEventListener(StatusEvent.STATUS, camHandler);
            ss.attachCamera(cam);
            myVideo.attachCamera(cam);                  
            microphone = Microphone.getMicrophone();
            setMicrophoneLevel();
            ss.attachAudio(microphone);
        }

        private function camHandler(e:StatusEvent):void{
            if(e.level == "status" && e.code == "Camera.Muted")ExternalInterface.call("p2pError", "allowcamaccess");
        }

        public function ncStatus(event:NetStatusEvent):void{
            ExternalInterface.call("p2pError",event.info.code);
            if(event.info.code == "NetConnection.Connect.Success"){
                myPeerID = nc.nearID;
                initSendStream();
                ExternalInterface.call("alert",nc.nearID);
            }
        }

        private function initSendStream():void{
            ss = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
            setupCamera();
            ss.publish('chat');
            var client:Object = new Object();
            client.onPeerConnect = function(subscriber:NetStream):Boolean {
                if(!sendStreams[subscriber.farID])sendStreams[subscriber.farID] = subscriber;
                if(!recvStreams[subscriber.farID])initReceiveStream(subscriber.farID);
                if(!recvVideos[subscriber.farID]){
                    try{
                        recvVideos[subscriber.farID] = getVideoBox();//truoble somewhere here
                        recvVideos[subscriber.farID].attachNetStream(subscriber);//truoble somewhere here
                    }
                    catch(e:Error){ExternalInterface.call("tester", e); }
                }
                return true;
            }
            ss.client = client;
        }

        private function initReceiveStream(peerID:String):void{
            if(peerID){
                rs = new NetStream(nc,peerID);
                rs.play('chat');
                var client:Object = new Object();
                client.receiveSomeData = receiveSomeData;
                rs.client = client;
                var peer:Object = new Object();
                peer.stream = rs;
                recvStreams[peerID] = peer;
            }
        }

        private function sendSomeData(str:String,farIds:String):void{
            if(str!=null&&str!=""){
                str = str.replace(/(^[\r\n\t\s]+)|([\r\n\t\s]$)/g,"");
                farIds = farIds == null ? "" : farIds.replace(/[^a-z0-9;]/gi,"");
                if(farIds!=""){
                    var farId:Array = farIds.split(";");
                    for(var i:int;i<farId.length;i++){
                        if(farId[i]&&sendStreams[farId[i]]){
                            sendStreams[farId[i]].send('receiveSomeData', str, myPeerID);
                        }
                    }
                }
                else{
                    for(var id:String in sendStreams){
                        sendStreams[id].send('receiveSomeData', str, myPeerID);
                    }
                }
            }
        }

        private function receiveSomeData(str:String, farId:String):void{
            ExternalInterface.call("receiveSomeData", str, farId);
        }
    ]]>
</mx:Script>

javascript:

    var name='myflash';

function getP2p(){
    if(navigator.appName.indexOf("Microsoft")!=-1)return window[name];
    else return document[name];
}

function p2pStartInit(){
    try{getP2p().initConnection()}
    catch(e){p2pError('flasherror')}
}

function initReceiveStream(p2pId){
    try{getP2p().initReceiveStream(p2pId)}
    catch(e){p2pError(e)}
}

function sendSomeData(str,ids){
    try{getP2p().sendSomeData(str,ids)}
    catch(e){p2pError(e)}
}

function p2pError(err){
    alert(err)
}

function receiveSomeData(str,id){
    alert('Received:'+str+'/'+id)
}

function tester(p,hf){
    var err=getProps(p,hf)
    alert(err.substr(0,err.length-1))
}
function getProps(obj,hf,n,t){
    if(!n)n='\n';
    if(!t)t=''
    if(t.match(/&nbsp;/))t+='&nbsp;&nbsp;&nbsp;'
    else t+='\t'
    //if(!hf)hf=1
    var s='';
    for(var i in obj){
        if(hf&&typeof obj[i]=='object')s+=t+i+':'+getProps(obj[i],hf,'',t)+n
        else s+=t+i+':'+obj[i]+','+n;
    }
    s='{'+n+s+t.substr(0,t.length-1)+'},';
    return s;
}
function getRandom(min,max){
    var r=Math.random();
    if(r==0)r=1;
    return Math.floor(r*(max-min)+min)
}

如果我在 initSendStream 函数中删除这些部分,一切正常:

recvVideos[subscriber.farID] = getVideoBox();
recvVideos[subscriber.farID].attachNetStream(subscriber);

我可以向任何我想要的同伴发送消息,我可以从我的网络摄像头看到视频。但是当我向任何人发送消息时,我也需要发送视频,我的问题就从这里开始了。

如果我用这两行代码编译应用程序,一切都会正常工作,直到我尝试连接到另一个对等方。这里出现错误 #1009,视频无法在对等方之间传输。我不明白我做错了什么。

还有另一个问题,可能是解决方案可能隐藏在这里。它一直出现。我在两个浏览器中测试这个聊天 - mozilla 和 opera。如果带有应用程序的页面首先在 opera 中加载,那么我可以在 opera 中看到来自网络摄像头的视频,而在 mozilla 中看不到它。如果 mozilla 页面首先加载,那么我可以在 mozilla 中看到视频,而在 opera 中看不到视频。为什么会这样?这真的让我很困惑!

所以请大家告诉我的代码有什么问题!

非常感谢您的帮助!

4

1 回答 1

0

done! errors were at initReceiveStream function. there was no rs.receiveVideo(true); and when i declered recvVideos the right way was private var recvVideos:Object = new Object();

于 2013-04-27T23:15:55.297 回答