Search code examples
flashflex4streamingaudio-streamingred5

How to have 2 way interaction in audio through red5 server


I am a newbie in Flash development.I am trying to create a flash(i have done that and can save the stream to red 5 server) in which i have the option to publish my own microphone stream to red5 server and also, i have the option to listen to a already published stream.This same flash i will be using on two different machines.I will publish the microphone from both the machines by giving the streams different names.Then i will try to open the streams from opposite machines so that i can have the 2 way audio chatting from the 2 machines.

e.g from machine 1 the stream published was Stream1. from machine 2 the stream published was Stream2.

The stream played from machine 1 was Stream2.
The stream played from machine 2 was Stream1.

The problem which i am facing is that i have the difficulty to attain the 2 way communication .That is i am not able to hear the stream 2 from 1st machine.When i publish the stream from second machine then my first stream gets disconnected and is overridden by second stream. Can anyone give me a proper suggestion for it,to how to rectify this to attain a gud 2 way connection or how can i stream 2 audios together.


Solution

  •                   Well, to have an audio chatting application using red5 and flex 4.5 you can try the code bellow. It should be tuned to your purpose of course:

    Audio Chat machine 1          

        <mx:Script>
         <![CDATA[
             import mx.controls.Alert;
             
             private var netConnection:NetConnection;
             private var InsertStream:NetStream;
             private var getStream:NetStream;
             private var connectionUrl:String="rtmp://YOURSERVER/vchat";
             
             private function init():void
             {
                 netConnection=new NetConnection();
                 netConnection.connect(connectionUrl);
                 netConnection.addEventListener(NetStatusEvent.NET_STATUS,connectHandler);
             }                                   
             
             private function connectHandler(e:NetStatusEvent):void
             {
              if(e.info.code=="NetConnection.Connect.Success")
              {                                            
                InsertStream=new NetStream(netConnection);
                  InsertStream.attachAudio(Microphone.getMicrophone());
                  InsertStream.publish("stream1","live");
                  getStream=new NetStream(netConnection);
                  getStream.attachAudio(Microphone.getMicrophone());
                  getStream.play("stream2"); // play the machine 2 stream
              }
              else
              {
                  Alert.show("server Problem");
              }                                
      }
     ]]>
     </mx:Script>
     
    

    Machine 2

        <mx:Script>
         <![CDATA[
             import mx.controls.Alert;
             
             private var netConnection:NetConnection;
             private var InsertStream:NetStream;
             private var getStream:NetStream;
             private var connectionUrl:String="rtmp://YOURSERVER/vchat";
             
             private function init():void
             {
                 netConnection=new NetConnection();
                 netConnection.connect(connectionUrl);
                 netConnection.addEventListener(NetStatusEvent.NET_STATUS,connectHandler);
             }                                   
             
             private function connectHandler(e:NetStatusEvent):void
             {
              if(e.info.code=="NetConnection.Connect.Success")
              {                                            
                  InsertStream=new NetStream(netConnection);
                  InsertStream.attachAudio(Microphone.getMicrophone());
                  InsertStream.publish("stream2","live");
                  getStream=new NetStream(netConnection);
                  getStream.attachAudio(Microphone.getMicrophone());
                  getStream.play("stream1"); // play stream from the other machine
              }
              else
              {
                  Alert.show("server Problem");
              }                                
      }
     ]]>
     </mx:Script>