基于FMS 的AS3录音机(转)

转:http://hi.baidu.com/dn_web/blog/item/7c8765d6af0823d4a144df91.html

基本具备的功能,录音,暂停录音,重新录音,播放录音,暂停播放录音,以及停止播放。

 

代码如下:

核心录音

package com.DNight
{
import flash.events.EventDispatcher;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.utils.setInterval;
import flash.utils.clearInterval;
import com.DNight.events.RecorderEvent
/**
* ...
* @author DN
*/
public class Recorder extends EventDispatcher
{
   internal static const FMSSTR:String = "rtmp://localhost/FMSRecord";
   public static const FMS_TOTAL_TIME:int = 10;
   private var _netConnection:NetConnection;
   private var _netStream:NetStream;
   private var _mic:Microphone;
  
   private var _recordRember:int = 0;
   private var _recordName:String;
   private var _recordingTime:int = 0;
   private var _intervalId:uint;
  
   public function get mic():Microphone {
    return _mic
   }
   public function get recordName():String {
    return _recordName;
   }
   public function Recorder()
   {
    _netConnection = new NetConnection();
    _netConnection.connect(Recorder.FMSSTR);
    _netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandle);
   
    _mic = Microphone.getMicrophone();
    _mic.gain = 60;
    _mic.rate = 11;
    _mic.setUseEchoSuppression(true);
    _mic.setLoopBack(true);
    _mic.setSilenceLevel(5, 1000);
   }
  
   private function onNetStatusHandle(event:NetStatusEvent) {
    switch(event.info.code) {
     case "NetConnection.Connect.Success":
     trace("连接成功");
     _netStream = new NetStream(_netConnection);
    
     break
     case "NetConnection.Connect.Failed":
     trace("连接失败");
     break
    }
   }
  
   public function startRecord() {
    if (_recordRember == 0) {
     //trace("第一次开始录制");
     var nowTime:Date = new Date();
     _recordName = nowTime.getTime().toString();
     _netStream.attachAudio(_mic);
     _netStream.publish(_recordName,"record");
     _recordRember = 1;
    }else {
     //trace("继续录制");
     _netStream.publish(_recordName,"append");
    }
    _intervalId=setInterval(upData,1000);
   }
  
   public function pauseRecord() {
    //trace("暂停录制");
    _netStream.close();
    clearInterval(_intervalId);
   }
  
   public function stopRecord() {
    //trace("停止录制,重新录制");
    _netStream.close();
    _recordRember = 0;
    clearInterval(_intervalId);
    _recordingTime = 0;
   }
  
   private function upData() {
    _recordingTime++;
    if (_recordingTime<=Recorder.FMS_TOTAL_TIME) {
     var recordProgress:RecorderEvent = new RecorderEvent(RecorderEvent.RECORD_PROGRESS);
     this.dispatchEvent(recordProgress);
    }else {
     var recordEnd:RecorderEvent = new RecorderEvent(RecorderEvent.RECORD_END);
     this.dispatchEvent(recordEnd);
     clearInterval(_intervalId);
     _recordingTime = 0;
    }
   }
}

}

核心播放

package com.DNight
{
import adobe.utils.CustomActions;
import com.DNight.events.PlayEvent;
import flash.events.EventDispatcher;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.media.SoundMixer
/**
* ...
* @author DN
*/
public class Player extends EventDispatcher
{
   private var _path:String
   private var _video:Video;
   private var _netConnection:NetConnection;
   private var _netStream:NetStream;
   private var _duration:Number;
   private var _time:Number = 0;
  
   public function get duration() {
    return _duration
   }
   public function get stream() {
    return _netStream
   }
   public function Player(newPath)
   {
    _path = newPath;
    _video = new Video();
    _netConnection = new NetConnection();
    _netConnection.connect(Recorder.FMSSTR);
    _netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandle);
   }
  
   private function netStatusHandle(eve:NetStatusEvent) {
    switch(eve.info.code) {
     case "NetConnection.Connect.Success":
     _netStream = new NetStream(_netConnection);
     _netStream.bufferTime = 3;
     var client:Object = new Object();
     client.onMetaData = onMetaData;
     client.onPlayStatus = onPlayStatus;
     _netStream.client = client;
     break
    }
   }
  
   private function onMetaData(data:Object) {
    _duration = data.duration;
    var event:PlayEvent = new PlayEvent(PlayEvent.GET_DUREATON);
    dispatchEvent(event);
    //trace("音频长度"+_duration);
   }
  
   private function onPlayStatus(data:Object) {
    switch(data.code) {
     case "NetStream.Play.Complete":
     var event:PlayEvent = new PlayEvent(PlayEvent.PLAY_END);
     dispatchEvent(event);
     _time = 0;
     break
    }
   }
  
   public function play() {
    if (_time == 0) {
     _netStream.play(_path);
    }else {
     _netStream.resume();
    }
   }
  
   public function pause() {
    _time = _netStream.time;
    _netStream.pause();
   }
  
   public function stop() {
    trace("停止音乐");
    _netStream.close();
    SoundMixer.stopAll();
   }
}

}

管理(主类):

package com.DNight
{
import flash.display.Sprite;
import flash.events.Event
import flash.events.MouseEvent;
import com.DNight.events.RecorderEvent
import com.DNight.events.PlayEvent
import flash.media.Microphone;
import flash.net.NetStream;
/**
* ...
* @author DN
*/
public class RecordPlayer extends Sprite
{
   private var _recording:Boolean;
   private var _recorder:Recorder;
   private var _mic:Microphone
   private var _recordingTime:int = 0;
   private var _line:Sprite
   private var _data:Array;
  
   private var _playing:Boolean
   private var _player:Player;
   private var _duration:Number = 0;
   private var _nowTime:Number = 0;
   private var _stream:NetStream
   public function RecordPlayer()
   {
    this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);
   }
  
   private function onAddToStageHandle(event:Event) {
    this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
    resetBtn.gotoAndStop("notdo");
    playBtn.gotoAndStop("notdo");
    stopBtn.gotoAndStop("notdo");
    _recorder = new Recorder();
    _recorder.addEventListener(RecorderEvent.RECORD_PROGRESS, onRecorderProgressHandle);
    _recorder.addEventListener(RecorderEvent.RECORD_END, onRecorderEndHandle);
    _mic = _recorder.mic
   }
  
   private function onrecordBtnClickHandle(event:MouseEvent) {
    _recording = !_recording;
    if (_recording) {
     recordBtn.gotoAndStop("pause");
     resetBtn.gotoAndStop("cando");
     playBtn.gotoAndStop("notdo");
     resetBtn.addEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
     playBtn.removeEventListener(MouseEvent.CLICK, onPlayBtnClickHandle);
     //开始录制
     _recorder.startRecord();
     _data = new Array(308);
     this.addEventListener(Event.ENTER_FRAME, onReocrdingHandle);
     //播放器初始化
     _player = new Player(_recorder.recordName);
     _player.addEventListener(PlayEvent.GET_DUREATON, onGetDurationHandle);
     _player.addEventListener(PlayEvent.PLAY_END,onPlayEndHandle);
    }else {
     recordBtn.gotoAndStop("record");
     _recorder.pauseRecord();
     this.removeEventListener(Event.ENTER_FRAME, onReocrdingHandle);
    
     //播放
     playBtn.gotoAndStop("cando");
     playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
    }
   }
  
   private function onResetBtnClickHandle(event:MouseEvent) {
    _recording = false
    recordBtn.gotoAndStop("record");
    resetBtn.gotoAndStop("notdo");
    playBtn.gotoAndStop("notdo");
    resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
    playBtn.removeEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
    _recorder.stopRecord();
    this.wave.bar.width = 0;
    this.wave.line.graphics.clear();
    _data.length = 0;
    _recordingTime = 0;
    this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);
   }
  
   private function onRecorderProgressHandle(event:RecorderEvent) {
    _recordingTime++;
    this.wave.bar.width = _recordingTime / Recorder.FMS_TOTAL_TIME * 320;
    trace("录制中"+_recordingTime);
   }
  
   private function onReocrdingHandle(event:Event) {
    this.wave.line.graphics.clear();
    this.wave.line.graphics.lineStyle(1, 0x00ff00, 1);
    this.wave.line.graphics.moveTo(6, 0);
    _data.shift();
    _data.push(_mic.activityLevel);
    for (var i:Number = 0; i < 308; i++) {
     this.wave.line.graphics.lineTo(6+i, - _data[i]/6);
    }
   }
  
   private function onRecorderEndHandle(event:RecorderEvent) {
   // trace("垆埴完毕");
    _recording = false;
    recordBtn.gotoAndStop("record");
    resetBtn.gotoAndStop("notdo");
    playBtn.gotoAndStop("cando");
    resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
    playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
    _recorder.stopRecord();
    _recordingTime = 0;
    this.wave.bar.width = 0;
    this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);
   }
  
   private function onPlayBtnClickHandle(event:MouseEvent) {
    _playing = !_playing;
    if (_playing) {
     //trace("播放");
     recordBtn.gotoAndStop("notdo");
     resetBtn.gotoAndStop("notdo");
     playBtn.gotoAndStop("pause");
     stopBtn.gotoAndStop("cando");
     recordBtn.removeEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
     resetBtn.removeEventListener(MouseEvent.CLICK,onResetBtnClickHandle);
     resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
     stopBtn.addEventListener(MouseEvent.CLICK, onStopBtnClickHandle);
     //播放
     _player.play();
     this.addEventListener(Event.ENTER_FRAME, onPlayingHandle);
    
     this.wave.line.graphics.clear();
     _data.length = 0;
     //var musicWave:MusicWave = new MusicWave(320, 53);
     //this.wave.addChild(musicWave);
    }else {
     //trace("暂停");
     recordBtn.gotoAndStop("record");
     resetBtn.gotoAndStop("notdo");
     playBtn.gotoAndStop("cando");
     stopBtn.gotoAndStop("notdo");
     this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
     resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
     stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);
    
     _recording = false;
     _recorder.stopRecord();
     _recordingTime = 0;
     //暂停
     _player.pause();
     this.removeEventListener(Event.ENTER_FRAME,onPlayingHandle);
    }
   
   }
  
   private function onGetDurationHandle(event:PlayEvent) {
    _duration = _player.duration;
    _stream = _player.stream;
    this.wave.bar.width = 0;
    this.addEventListener(Event.ENTER_FRAME,onPlayingHandle);
   }
  
   private function onPlayEndHandle(event:PlayEvent) {
    this.removeEventListener(Event.ENTER_FRAME, onPlayingHandle);
    onStopBtnClickHandle(null);
    this.wave.bar.width = 0;
   }
  
   private function onPlayingHandle(event:Event) {
    _nowTime = _stream.time;
    if (this.wave.bar.width>=320) {
     this.wave.bar.width = 0;
    }else{
     this.wave.bar.width = _nowTime / _duration * 320;
    }
   }
   //播放停止
   private function onStopBtnClickHandle(event:MouseEvent) {
    //trace("播放停止");
    recordBtn.gotoAndStop("record");
    resetBtn.gotoAndStop("notdo");
    playBtn.gotoAndStop("cando");
    stopBtn.gotoAndStop("notdo");
    this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
    resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
    stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);
    _playing = false
    _recording = false;
    _recorder.stopRecord();
    _recordingTime = 0;
    //停止
    _player.stop();
   }

}

}

 

posted @   94cool  阅读(1053)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2009年11月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12
点击右上角即可分享
微信分享提示