订阅流和发布流都是基于RTMP协议传输的flv格式封装流。
此处的发布流,是通过麦克风采集的音频数据。
注意:经过亲测,两条流是基于两条分开的连接,否则不能双向传输。
<!-- AudioIntercom -->
<!-- two connections ,two streams ,each of them on one conneciton ,different directions-->
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import org.osmf.elements.AudioElement;
private var connPublish:NetConnection;
private var streamPublish:NetStream ;
private var connReceive:NetConnection ;
private var streamReceive:NetStream;
private var microphone:Microphone ;
private var video:Video = new Video() ;
private var serverAddress:String ;
private var publishStream:String ;
private var receiveStream:String ;
private const DELAY_LENGTH:int = 4000;
/**
* 初始化参数
*/
public function InitPlay():void
{
connReceive = new NetConnection();
connReceive.objectEncoding = ObjectEncoding.AMF0; //Compatible with low version
connReceive.addEventListener(NetStatusEvent.NET_STATUS, receiveNetStatusHandler);
connPublish = new NetConnection();
connPublish.objectEncoding = ObjectEncoding.AMF0; //Compatible with low version
connPublish.addEventListener(NetStatusEvent.NET_STATUS, publishNetStatusHandler);
btnPublish.enabled = true ;
btnReceive.enabled = true ;
}
/**
* 发布音频流
*/
protected function publishClickHandler(event:MouseEvent):void
{
if(textInputURL.text.length == 0 || textInputPublish.text.length == 0)
{
return ;
}
serverAddress = textInputURL.text;
publishStream = textInputPublish.text ;
connPublish.connect(serverAddress); //publish Connection
}
private function publishNetStatusHandler(event:NetStatusEvent):void
{
Alert.show(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
btnPublish.enabled = false ;
createPublishStream();
break;
case "NetConnection.Connect.Rejected":
break;
case "NetStream.Play.StreamNotFound":
break;
case "NetStream.Publish.Start":
var deviceArray:Array = new Array();
deviceArray = Microphone.names ;
Alert.show("deviceArray: "+deviceArray.toString()+" "+deviceArray.length);
break;
}
}
protected function createPublishStream():void
{
streamPublish = new NetStream(connPublish);
microphone = Microphone.getMicrophone();
microphone.codec = SoundCodec.SPEEX ;
microphone.setUseEchoSuppression(true);
//Microphone.getEnhancedMicrophone();
//microphone.setLoopBack(true); //回传到本地席通风扬声
microphone.setSilenceLevel(0,DELAY_LENGTH);
microphone.gain = 100;
microphone.rate = 44; //我们的设备音频采样率为8kHZ,可以默认为 8
streamPublish.addEventListener(NetStatusEvent.NET_STATUS,publishNetStatusHandler) ;
streamPublish.bufferTime = 0 ;
streamPublish.attachAudio(microphone);
streamPublish.publish(publishStream,"live");
}
/**
* 接收音视频流
*/
protected function receiveClickHandler(event:MouseEvent):void
{
if(textInputURL.text.length == 0 || textInputReceive.text.length == 0)
{
return ;
}
serverAddress = textInputURL.text;
receiveStream = textInputReceive.text ;
connReceive.connect(serverAddress); //play
}
protected function receiveNetStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
btnReceive.enabled = false ;
createReceiveStream();
break;
case "NetConnection.Connect.Rejected":
break;
case "NetStream.Play.Start":
btnReceive.label = "接收中";
break;
case "NetStream.Play.StreamNotFound":
break;
}
}
protected function createReceiveStream():void
{
var client:Object= new Object();
streamReceive = new NetStream(connReceive);
streamReceive.client = client;
client.onMetaData = netStream_onMetaData_Handler;
streamReceive.addEventListener(NetStatusEvent.NET_STATUS, receiveNetStatusHandler);
video.attachNetStream(streamReceive);
streamReceive.play(receiveStream);
}
/**
* onMetaData
*/
protected function netStream_onMetaData_Handler(info:Object):void
{
}
]]>
</fx:Script>