我正在构建一个语音聊天操作语音数据。我从麦克风得到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。第二个对等体在读取函数中获取它。会有东西解码它。现在它工作了,但我的扬声器里有很糟糕的常规声音。如果您能及时回复,我将不胜感激,提前表示感谢。
你的方法似乎是合理的,尽管我总是看到一个媒体服务器在流媒体源和接收器之间。您可以尝试设置Red5
(或类似),流式传输音频作为"现场"源,然后从另一边播放它。
虽然在传输中可能是 PCM(尽管有几种可用的压缩),但您可能会听到噪音,因为您错过了翻译的一个步骤。