0

我正在构建一个操纵语音数据的语音聊天。我从 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。第二个对等方正在读取功能中获取它。会有东西解码它。现在它正在工作,但我的扬声器中的声音很糟糕。我将不胜感激,并提前感谢。

4

1 回答 1

0

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

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

于 2012-12-29T13:49:14.000 回答