[FMS]AS3.0基于FMS写的录音机代码示例(3)
管理(主类): packagecom.DNight { importflash.display.Sprite; importflash.events.Event importflash.events.MouseEvent; importcom.DNight.events.RecorderEvent importcom.DNight.events.PlayEvent importflash
管理(主类):
- 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);
- //极酷cuplayer提示:开始录制
- _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();
- }
- }
- }
基本测试了下,不知道还有啥问题,日后在改,通过做这个录音机,了解了FMS的基本使用流程,以及熟悉了netStream的强大...
http://hi.baidu.com/enqkmhzhofbeirq/item/fc924ec680be230fc710b2f2
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...