3

我正在尝试建立一个 Flex 应用程序,该应用程序允许用户发送视频流并让其他人显示它。

我创建了一个发件人页面(如下)。代码非常简单并且完美运行:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            // Network properties
            private var connection:NetConnection;
            private var outStream:NetStream;
            private var RTMFP_CODE:String = "A_CODE";

            // Device properties    
            private var camera:Camera;
            private var microphone:Microphone;

            // Video properties
            private var outVideo:Video;

            // Flex components
            private var outVideoWrapper:UIComponent;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                connection = new NetConnection();    
                connection.connect("rtmfp://p2p.rtmfp.net/" + this.RTMFP_CODE);
                connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
            }

            private function onConnected(event:NetStatusEvent):void{
                if (event.info.code == "NetConnection.Connect.Success"){
                    setupVideo();
                }
            }

            private function setupVideo():void{
                // Setup outgoing devices
                camera = Camera.getCamera();
                microphone = Microphone.getMicrophone();

                // Setup outgoing stream

                outStream = new NetStream(connection);
                outStream.attachCamera(camera);
                outStream.attachAudio(microphone);
                outStream.publish("flex_rocks");

                // Setup outgoing video and attach outgoing devices
                outVideo = new Video();
                outVideo.attachCamera(camera);

                // Wrap the video object
                outVideoWrapper = new UIComponent;
                outVideoWrapper.addChild(outVideo);
                addElement(outVideoWrapper);
            }
        ]]>
    </fx:Script>
    <s:Button x="885" y="0" label="Send video" click="button1_clickHandler(event)"/>
</s:Application>

然后是接收方页面代码(如下):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            // Network properties
            private var connection:NetConnection;
            private var inStream:NetStream;
            private var RTMFP_CODE:String = "A_CODE";

            // Device properties
            private var camera:Camera;
            private var microphone:Microphone;

            // Video properties
            private var inVideo:Video;

            // Flex components

            // private var inVideoWrapper:UIComponent;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                connection = new NetConnection();
                connection.connect("rtmfp://p2p.rtmfp.net/" + this.RTMFP_CODE);
                connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
            }

            private function onConnected(event:NetStatusEvent):void{
                Alert.show(event.info.code);
                if (event.info.code == "NetConnection.Connect.Success"){
                    setupVideo();
                }
            }

            private function setupVideo():void{
                // Setup outgoing devices

                camera = Camera.getCamera();
                microphone = Microphone.getMicrophone();

                // Setup incoming stream
                inStream = new NetStream(connection);
                inStream.play("flex_rocks");

                // Setup incoming video and attach incoming stream
                inVideo = new Video();
                inVideo.attachNetStream(inStream);

                // Wrap the video object

                // inVideoWrapper = new UIComponent();   
                inVideoWrapper.addChild(inVideo);
                addElement(inVideoWrapper);
            }
        ]]>

    </fx:Script>
    <s:Button x="885" y="0" label="Receive a stream" click="button1_clickHandler(event)"/>
    <mx:UIComponent id="inVideoWrapper" x="0" y="0" width="500" height="500">
    </mx:UIComponent>    
</s:Application>

我不知道为什么在接收页面我无法显示传入的流?我放了一个简单的警报来显示返回的连接代码,它显示“成功”。

难道我做错了什么?

问候。

(PS:我在 Windows 7 64 位下使用 Flex Builder 4)。

4

1 回答 1

2

如果您将 Cirrus 用于 p2p,那么仅将两者都连接到 Cirrus 对您毫无帮助。需要发生的是连接到 Cirrus,从event.nearID您的 onConnected 处理程序中使用的服务获取所需的 ID。您还需要指定 Stream 以便能够像这样在 2 个客户端之间直接连接:

outStream = new NetStream(connection, NetStream.DIRECT_CONNECTIONS);

这就是事情变得有趣的地方,您现在需要将该 ID 传输给接收器。Cirrus 不会为您这样做。您需要有服务将其发送过来或手动输入。假设您确实手动输入了它,那么您的接收端需要使用以下方法进行连接:

inStream = new NetStream(connection, cirrusID);

反过来,这将把两个客户端直接连接到另一个客户端。

于 2011-03-29T16:53:53.950 回答