基于Flash Builder开发的流订阅与流发布

订阅流和发布流都是基于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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值