Second Physical Communication Laboratory

The sound live streaming

by admin on 1 月.21, 2009, under Showcase

This showcase is made the controller for the streaming live sound. You can change the parameters by the some slider bars.

showcase_2.swf

<?xml version="1.0"?>
<!-- containersnavigatorsTNSimple.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:TabNavigator borderStyle="solid" >
<mx:VBox label="Local"
width="400" height="400">
<!-- Accounts view goes here. -->

    <mx:Script>
        <![CDATA[
        import flash.media.Microphone;
        import flash.net.NetConnection;
        import flash.net.NetStream;
        import flash.events.*;
        import mx.events.SliderEvent;
        import mx.controls.sliderClasses.Slider;
        import flash.text.*;
        import flash.utils.Timer;
        import flash.geom.*;
        import flash.display.*;
        import mx.events.ItemClickEvent;

        public var timer:Timer;
        public var togle:Number;
        public var myMic:Microphone;
        public var pub_nc:NetConnection;
        public var pub_ns:NetStream;
        public var max:Number;
        public var counter:Number;
        private var streamingURL:String = "rtmp://wdash.iobb.net/oflaDemo";
        public function initCamera():void {
          publishVideo();
        }
        public function publishVideo():void{
          myMic = Microphone.getMicrophone();

          if (myMic!=null) {
            //ローカルスピーカーへのマイク音送信
            myMic.setLoopBack(true);
            //エコー抑制機能の使用
            myMic.setUseEchoSuppression(true);
            togle=0;
            myMic.gain= 50;
            //タイマーの追加
            timer=new Timer(20,0);
            timer.addEventListener(TimerEvent.TIMER,onTick);
            timer.start();
          }
          pub_nc = new NetConnection();
          pub_nc.client = new CustomClient();
          pub_nc.addEventListener(NetStatusEvent.NET_STATUS,pub_netStatus);
          pub_nc.connect(streamingURL);
          max = 1;
          counter = 0;

          RateLabel.text="Rate:"+ String(myMic.rate);
          MicrophoneNameLabel.text = "Mic:"+ String(myMic.name);
        }
        private function pub_netStatus(event:NetStatusEvent):void {
          if (event.info.code == "NetConnection.Connect.Success") {
            pub_ns = new NetStream(pub_nc);
            pub_ns.attachAudio(myMic);
            pub_ns.publish("demo","live");
          } else{
            trace("error");
          }
        }
        //タイマーリスナー
        public function onTick(event:TimerEvent):void {
          var g:Graphics = this.graphics;
          var temp:Number = myMic.activityLevel;
          var temp2:Number;
          LevelLabel.text= "Level:"+String(temp);
          var fillType:String = GradientType.LINEAR;
          var colors:Array = [0x00FF00,0xFF0000];
          var alphas:Array = [1, 1];
          var ratios:Array = [0x00, 0xFF];
          var matr:Matrix = new Matrix();
          matr.createGradientBox(40, 25, -90*Math.PI/180, 0, 0);
          var spreadMethod:String = SpreadMethod.PAD;
          myVid.graphics.clear();
          myVid.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
          myVid.graphics.drawRect(0,100,15,-temp);
               if(max < temp){
            max = temp;
            MaxLevelLabel.text="Max:"+ String(temp);
          }
          // 四角を描く
          myVid.graphics.drawRect(0,100-max-5,15,5);
          counter++;
          if(counter > 100){
            max = 0;
            MaxLevelLabel.text="Max:"+ String(temp);
            counter = 0;
          }
        }

        private function sliderChange(event:SliderEvent):void {
          var currentSlider:Slider=Slider(event.currentTarget);
          if(currentSlider.id == "SSlider"){
              myMic.gain= currentSlider.value;
          }
          else{
          }
        }
        private function LocalSpeakerChange():void {
          if(LocalSpeaker.selected){
            myMic.setLoopBack(true);
          }else{
            myMic.setLoopBack(false);
          }
        }
        private function EchoChange():void {
          if(Echo.selected){
            myMic.setUseEchoSuppression(true);
            togle=0;
          }else{
            myMic.setUseEchoSuppression(false);
            togle=0;
          }
        }
        public function handleClick():void {
          SSlider.value=1;
          LocalSpeaker.selected = true;
          Echo.selected = true;
          myMic.setLoopBack(true);
          myMic.setUseEchoSuppression(true);
          myMic.rate=8;
          togle=0;
        }
        public function RateChange(event:ItemClickEvent):void {
          if(event.currentTarget.selectedValue == "5"){
              myMic.rate= 5;
          }
          else if(event.currentTarget.selectedValue == "8"){
              myMic.rate= 8;
          }
          else if(event.currentTarget.selectedValue == "11"){
              myMic.rate= 11;
          }
          else if(event.currentTarget.selectedValue == "22"){
              myMic.rate= 22;
          }
          else if(event.currentTarget.selectedValue == "44"){
              myMic.rate= 44;
          }else{
          }
          RateLabel.text="Rate:"+ String(myMic.rate);
        }
        ]]>
    </mx:Script>
  


<mx:HBox borderStyle="solid" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
    <mx:VBox borderStyle="solid" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
        <mx:Label id="MicrophoneNameLabel" text="Mic:" x="10" y="10" width="150" />
        <mx:Label id="RateLabel" text="Rate:" x="10" y="10" width="150" />
        <mx:Label id="LevelLabel" text="Level:0" x="10" y="10" width="150" />
        <mx:Label id="MaxLevelLabel" text="Max:0" x="10" y="10" width="150" />
        <mx:Canvas id="myVid" width="100" height="100" creationComplete="initCamera();"/>
        
        <mx:Button id="button1" label="reset" width="150" fontSize="12" click="handleClick();"/>
    </mx:VBox>
   <mx:VBox borderStyle="solid" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
      <mx:Label id="SSLabel" text="Microphone gain:{SSlider.value}" x="10" y="138"/><mx:HSlider id="SSlider" change="sliderChange(event);"  liveDragging="true" tickInterval="10" snapInterval="1"  x="164" y="10" height="10" maximum="100" value="50" />
      <mx:CheckBox id="LocalSpeaker" change="LocalSpeakerChange();" width="150" label="Local Speaker on or off?" selected="true"/>
      <mx:CheckBox id="Echo" width="150"  change="EchoChange();" label="Echo gain" selected="true"/>
    <mx:RadioButtonGroup id="rate" itemClick="RateChange(event);" />
    <mx:VBox borderStyle="solid" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
      <mx:Label id="rateBLabel" text="Sampling mode." x="10" y="10"/>
      <mx:RadioButton groupName="rate" id="rate5" value="5" label="5kHz" width="150"/>
      <mx:RadioButton groupName="rate" id="rate8" value="8" label="8kHz" width="150" selected="true"/>
      <mx:RadioButton groupName="rate" id="rate11" value="11" label="11kHz" width="150"/>
      <mx:RadioButton groupName="rate" id="rate22" value="22" label="22kHz" width="150"/>
      <mx:RadioButton groupName="rate" id="rate44" value="44" label="44kHz" width="150"/>
    </mx:VBox>
   </mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:TabNavigator>
</mx:Application>
No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.