利用FMS实现的在线录音系统。

这是as3源代码:

package {
 import flash.display.Sprite;
 import flash.events.*;
 import flash.media.Microphone;
 import flash.system.Security;
 import flash.net.NetConnection;
 import flash.events.NetStatusEvent;
 import flash.net.NetStream;
 import flash.external.ExternalInterface;
 import flash.text.TextField;
 import flash.text.TextFieldType;
 import flash.text.TextFieldAutoSize;

 //import System.security.allowDomain("*");

 

 public class startphone extends Sprite {
  var nc:NetConnection;
  var ns:NetStream;
  var mic:Microphone;
  var videoName:String;
  var serverIp:String;

  public function startphone() {
   stopBtn.addEventListener(MouseEvent.CLICK, stopHandler);
   playbtn.addEventListener(MouseEvent.CLICK, connectStream);

   btnPlay.addEventListener(MouseEvent.CLICK,playRecord);
  }
  
  
  private function connectStream(event:MouseEvent):void {
   if(ExternalInterface.available)
   {
    var serverIp = ExternalInterface.call("getServerIP");
   }
   var rtmpUrl:String ="rtmp://"+serverIp+"/startphone";
   trace("Okay, let's connect now");
   nc=new NetConnection();
   nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
   nc.connect(rtmpUrl);
   /*if(ExternalInterface.available)
   {
    var LoginID = ExternalInterface.call("GetLoginID");
   }*/
   
   
   lbltishi.text = "开始录音";
   //lblTime.text = LoginID;
   //lblTime.text = "nihao";
   lblPlay.enabled = false;
   ns=new NetStream(nc);
   ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
   ns.client = new CustomClient();
   ns.play(videoName, 0);

  }
  
  
  private function playRecord(evt:MouseEvent):void {
   if(ExternalInterface.available)
   {
    var serverIp = ExternalInterface.call("getServerIP");
   }
   var rtmpUrl:String ="rtmp://"+serverIp+"/startphone";
   nc=new NetConnection();
   //nc.client=new Client();
   nc.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);
   nc.connect(rtmpUrl);
  }

 

 private function onPlayStatusHandler(evt:NetStatusEvent):void {
   if(ExternalInterface.available)
   {
    var LoginID = ExternalInterface.call("GetLoginID");
   }
   videoName = LoginID;
   if (evt.info.code=="NetConnection.Connect.Success") {
    lblPlay.text="正在播放录音";
    lbltishi.enabled = false;
    ns=new NetStream(nc);
    ns.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);
    ns.play(videoName,0);
   }
  }

 

 private function stopHandler(event:MouseEvent):void {
   lbltishi.text = "停止录音";
   trace("Now we're disconnecting");
   nc.close();
  }

  

private function netStatusHandler(event:NetStatusEvent):void {
   trace("connected is: " + nc.connected );
   trace("event.info.level: " + event.info.level);
   trace("event.info.code: " + event.info.code);

   switch (event.info.code) {
    case "NetConnection.Connect.Success" :
     trace("Congratulations! you're connected");
     publishLiveStream();
     break;
    case "NetConnection.Connect.Rejected" :
     trace("Oops! the connection was rejected");
     break;
    case "NetStream.Play.Stop" :
     trace("The stream has finished playing");
     break;
    case "NetStream.Play.StreamNotFound" :
     trace("The server could not find the stream you specified");
     break;
    case "NetStream.Publish.Start" :

     trace("Adding metadata to the stream");
     // when publishing starts, add the metadata to the stream
     var metaData:Object = new Object();
     metaData.title="myStream";
     metaData.width=400;
     metaData.height=200;
     ns.send("@setDataFrame", "onMetaData", metaData);
     break;

    case "NetStream.Publish.BadName" :
     trace("The stream name is already used");
     break;
   }
  }

 


  private function publishLiveStream():void {
   if(ExternalInterface.available)
   {
    var LoginID = ExternalInterface.call("GetLoginID");
   }
   
   ns=new NetStream(nc);
   ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
   ns.client = new CustomClient();
   mic=Microphone.getMicrophone();/* 声明声音,取得麦克风的声音*/
   videoName = LoginID;
   

   if (mic!=null) {
    mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
    mic.addEventListener(StatusEvent.STATUS, statusHandler);
    ns.attachAudio(mic);/* 把声音加入到流中*/
    ns.publish(videoName, "record");/* 发布声音*/

   }
  }

  private function activityHandler(event:ActivityEvent):void {
   trace("activityHandler: " + event);
  }

  private function statusHandler(event:StatusEvent):void {
   trace("statusHandler: " + event);
  }
  
  
  private function getNowTime() : String
        {
            var _loc_1:* = new Date();
            var _loc_2:* = _loc_1.getFullYear() > 9 ? (_loc_1.getFullYear().toString()) : ("0" + _loc_1.getFullYear().toString());
            var _loc_3:* = _loc_1.getMonth() + 1 > 9 ? ((_loc_1.getMonth() + 1).toString()) : ("0"+(_loc_1.getMonth() + 1).toString());
            var _loc_4:* = _loc_1.getDate() > 9 ? (_loc_1.getDate().toString()) : ("0" + _loc_1.getDate().toString());
            var _loc_5:* = _loc_1.getHours() > 9 ? (_loc_1.getHours().toString()) : ("0" + _loc_1.getHours().toString());
            var _loc_6:* = _loc_1.getMinutes() > 9 ? (_loc_1.getMinutes().toString()) : ("0" + _loc_1.getMinutes().toString());
            var _loc_7:* = _loc_1.getSeconds() > 9 ? (_loc_1.getSeconds().toString()) : ("0" + _loc_1.getSeconds().toString());
            return _loc_2 + _loc_3 + _loc_4  + _loc_5 + _loc_6 + _loc_7;
        }// end function

 }
}
class CustomClient {
 

public function onMetaData(info:Object):void {
  trace("width: " + info.width);
  trace("height: " + info.height);
 }
 public function onPlayStatus(info:Object):void {
  trace("handling playstatus here");
 }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值