通过RTMFP在ByteArray中发送语音

时间:2012-12-29 13:31:59

标签: actionscript-3 flex netstream voice-recording

我正在构建一个操纵语音数据的语音聊天。我从Microphone获取SAMPLE_DATA(ByteArray)并通过NetStream send()方法发送它。然后我必须将其转换回声音。我该怎么办?

这是建立连接,捕获和发送语音数据的类(我知道这很丑,但它是临时版本)。

package app.net {

public class CirrusConnection extends EventDispatcher {
    private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/";
    private const DEVELOPER_KEY:String = "bla-bla-bla";

    // Net objects ~~~~~~~~~~
    private var nc:NetConnection;

    private var sendStream:NetStream;
    private var recvStream:NetStream;

    private var myPeerID:String;
    private var farPeerID:String;

    // User output handler
    private var chatTextArea:TextArea;

    //---
    private var mic:Microphone;
    //---

    public function CirrusConnection() { }

    public function initConnection():void {
        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        nc.connect(SERVER_ADDRESS + DEVELOPER_KEY);

        mic = Microphone.getMicrophone();
        mic.rate = 8;
        mic.setUseEchoSuppression(true);
        mic.gain = 50;
    }

    private function netStatusHandler(event:NetStatusEvent):void {
        trace("CirrusConnection: " + event.info.code);
        switch(event.info.code) {
            case "NetConnection.Connect.Success":
                myPeerID = nc.nearID;
                openSendStream();
                dispatchEvent(new Event(NetEvent.CIRRUS_SENT_KEY));
                break;
            case "NetStream.Play.Start":
                dispatchEvent(new Event(NetEvent.CLIENT_IS_CALLING ));
                break;
        }
    }

    public function getNetConnection():NetConnection {
        return nc;
    }

    public function getMyPeerID():String {
        return myPeerID;
    }

    public function isConnected():Boolean {
        return Boolean(myPeerID);
    }

    public function setChatTextArea(chatTextArea:TextArea):void {
        this.chatTextArea = chatTextArea;
    }

    private function openSendStream():void {
        var sendStreamClient:Object = new Object();
        sendStreamClient.onPeerConnect = function(callerns:NetStream):Boolean {
            if (!Boolean(farPeerID)) {
                connectToPeer(callerns.farID);
            }
            return true;
        }

        sendStream = new NetStream(this.nc, NetStream.DIRECT_CONNECTIONS);
        sendStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        sendStream.client = sendStreamClient;
        sendStream.publish("media");
    }

    public function connectToPeer(id:String):void {
        farPeerID = id;
        recvStream = new NetStream(nc,farPeerID);
        recvStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
        recvStream.client = this;
        recvStream.play("media");
    }

    public function read(data:ByteArray):void {
        var snd:Sound = new Sound();
        data.position = 0;
        snd.loadPCMFromByteArray(data, data.length >> 3);
        snd.play();
    }

    public function beginStreamMicrophoneData():void {
        mic.addEventListener("sampleData", streamData);
    }

    public function streamData(event:SampleDataEvent):void {
        sendStream.send("read", event.data as ByteArray );
    }

}

}

当捕获麦克风数据时,将调用streamData函数。它将ByteArray发送给第二个同行。第二个同伴正在读取它的功能。将有解码它的东西。现在它正在工作,但我的扬声器发出可怕的常规声音。我将非常感谢您的快速反应,我提前感谢您。

1 个答案:

答案 0 :(得分:0)

虽然我总是在流媒体源和接收器之间看到媒体服务器,但您的方法似乎是合理的。您可以尝试设置Red5(或类似),将音频作为“实时”源播放,然后从另一侧播放。

虽然传输中的可能是 PCM(尽管有几种压缩方式可用),但是你可能会因为错过翻译步骤而听到噪音。