·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[FMS]AS3.0基于FMS写的录音机代码示例(3)

时间:2012-09-20 17:10虫子的窝
管理(主类): packagecom.DNight { importflash.display.Sprite; importflash.events.Event importflash.events.MouseEvent; importcom.DNight.events.RecorderEvent importcom.DNight.events.PlayEvent importflash

管理(主类):

  1. package com.DNight   
  2. {  
  3. import flash.display.Sprite;  
  4. import flash.events.Event  
  5. import flash.events.MouseEvent;  
  6. import com.DNight.events.RecorderEvent  
  7. import com.DNight.events.PlayEvent  
  8. import flash.media.Microphone;  
  9. import flash.net.NetStream;  
  10. /**  
  11. * ...  
  12. * @author DN  
  13. */  
  14. public class RecordPlayer extends Sprite  
  15. {  
  16. private var _recording:Boolean;  
  17. private var _recorder:Recorder;  
  18. private var _mic:Microphone  
  19. private var _recordingTime:int = 0;  
  20. private var _line:Sprite  
  21. private var _data:Array;  
  22.  
  23. private var _playing:Boolean  
  24. private var _player:Player;  
  25. private var _duration:Number = 0;  
  26. private var _nowTime:Number = 0;  
  27. private var _stream:NetStream  
  28. public function RecordPlayer()   
  29. {  
  30. this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);  
  31. }  
  32.  
  33. private function onAddToStageHandle(event:Event) {  
  34. this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);  
  35. resetBtn.gotoAndStop("notdo");  
  36. playBtn.gotoAndStop("notdo");  
  37. stopBtn.gotoAndStop("notdo");  
  38. _recorder = new Recorder();  
  39. _recorder.addEventListener(RecorderEvent.RECORD_PROGRESS, onRecorderProgressHandle);  
  40. _recorder.addEventListener(RecorderEvent.RECORD_END, onRecorderEndHandle);  
  41. _mic = _recorder.mic  
  42. }  
  43.  
  44. private function onrecordBtnClickHandle(event:MouseEvent) {  
  45. _recording = !_recording;  
  46. if (_recording) {  
  47. recordBtn.gotoAndStop("pause");  
  48. resetBtn.gotoAndStop("cando");  
  49. playBtn.gotoAndStop("notdo");  
  50. resetBtn.addEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  51. playBtn.removeEventListener(MouseEvent.CLICK, onPlayBtnClickHandle);  
  52. //极酷cuplayer提示:开始录制  
  53. _recorder.startRecord();  
  54. _data = new Array(308);  
  55. this.addEventListener(Event.ENTER_FRAME, onReocrdingHandle);  
  56. //播放器初始化  
  57. _player = new Player(_recorder.recordName);  
  58. _player.addEventListener(PlayEvent.GET_DUREATON, onGetDurationHandle);  
  59. _player.addEventListener(PlayEvent.PLAY_END,onPlayEndHandle);  
  60. }else {  
  61. recordBtn.gotoAndStop("record");  
  62. _recorder.pauseRecord();  
  63. this.removeEventListener(Event.ENTER_FRAME, onReocrdingHandle);  
  64.  
  65. //播放  
  66. playBtn.gotoAndStop("cando");  
  67. playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);  
  68. }  
  69. }  
  70.  
  71. private function onResetBtnClickHandle(event:MouseEvent) {  
  72. _recording = false 
  73. recordBtn.gotoAndStop("record");  
  74. resetBtn.gotoAndStop("notdo");  
  75. playBtn.gotoAndStop("notdo");  
  76. resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  77. playBtn.removeEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);  
  78. _recorder.stopRecord();  
  79. this.wave.bar.width = 0;  
  80. this.wave.line.graphics.clear();  
  81. _data.length = 0;  
  82. _recordingTime = 0;  
  83. this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);  
  84. }  
  85.  
  86. private function onRecorderProgressHandle(event:RecorderEvent) {  
  87. _recordingTime++;  
  88. this.wave.bar.width = _recordingTime / Recorder.FMS_TOTAL_TIME * 320;  
  89. trace("录制中"+_recordingTime);  
  90. }  
  91.  
  92. private function onReocrdingHandle(event:Event) {  
  93. this.wave.line.graphics.clear();  
  94. this.wave.line.graphics.lineStyle(1, 0x00ff00, 1);  
  95. this.wave.line.graphics.moveTo(6, 0);  
  96. _data.shift();  
  97. _data.push(_mic.activityLevel);  
  98. for (var i:Number = 0; i < 308; i++) {  
  99. this.wave.line.graphics.lineTo(6+i, - _data[i]/6);  
  100. }  
  101. }  
  102.  
  103. private function onRecorderEndHandle(event:RecorderEvent) {  
  104. // trace("垆埴完毕");  
  105. _recording = false;  
  106. recordBtn.gotoAndStop("record");  
  107. resetBtn.gotoAndStop("notdo");  
  108. playBtn.gotoAndStop("cando");  
  109. resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  110. playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);  
  111. _recorder.stopRecord();  
  112. _recordingTime = 0;  
  113. this.wave.bar.width = 0;  
  114. this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);  
  115. }  
  116.  
  117. private function onPlayBtnClickHandle(event:MouseEvent) {  
  118. _playing = !_playing;  
  119. if (_playing) {  
  120. //trace("播放");  
  121. recordBtn.gotoAndStop("notdo");  
  122. resetBtn.gotoAndStop("notdo");  
  123. playBtn.gotoAndStop("pause");  
  124. stopBtn.gotoAndStop("cando");  
  125. recordBtn.removeEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);  
  126. resetBtn.removeEventListener(MouseEvent.CLICK,onResetBtnClickHandle);  
  127. resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  128. stopBtn.addEventListener(MouseEvent.CLICK, onStopBtnClickHandle);  
  129. //播放  
  130. _player.play();  
  131. this.addEventListener(Event.ENTER_FRAME, onPlayingHandle);  
  132.  
  133. this.wave.line.graphics.clear();  
  134. _data.length = 0;  
  135. //var musicWave:MusicWave = new MusicWave(320, 53);  
  136. //this.wave.addChild(musicWave);  
  137. }else {  
  138. //trace("暂停");  
  139. recordBtn.gotoAndStop("record");  
  140. resetBtn.gotoAndStop("notdo");  
  141. playBtn.gotoAndStop("cando");  
  142. stopBtn.gotoAndStop("notdo");  
  143. this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);  
  144. resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  145. stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);  
  146.  
  147. _recording = false;  
  148. _recorder.stopRecord();  
  149. _recordingTime = 0;  
  150. //暂停  
  151. _player.pause();  
  152. this.removeEventListener(Event.ENTER_FRAME,onPlayingHandle);  
  153. }  
  154. }  
  155. private function onGetDurationHandle(event:PlayEvent) {  
  156. _duration = _player.duration;  
  157. _stream = _player.stream;  
  158. this.wave.bar.width = 0;  
  159. this.addEventListener(Event.ENTER_FRAME,onPlayingHandle);  
  160. }  
  161. private function onPlayEndHandle(event:PlayEvent) {  
  162. this.removeEventListener(Event.ENTER_FRAME, onPlayingHandle);  
  163. onStopBtnClickHandle(null);  
  164. this.wave.bar.width = 0;  
  165. }  
  166. private function onPlayingHandle(event:Event) {  
  167. _nowTime = _stream.time;  
  168. if (this.wave.bar.width>=320) {  
  169. this.wave.bar.width = 0;  
  170. }else{  
  171. this.wave.bar.width = _nowTime / _duration * 320;  
  172. }  
  173. }  
  174. //播放停止  
  175. private function onStopBtnClickHandle(event:MouseEvent) {  
  176. //trace("播放停止");  
  177. recordBtn.gotoAndStop("record");  
  178. resetBtn.gotoAndStop("notdo");  
  179. playBtn.gotoAndStop("cando");  
  180. stopBtn.gotoAndStop("notdo");  
  181. this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);  
  182. resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);  
  183. stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);  
  184. _playing = false 
  185. _recording = false;  
  186. _recorder.stopRecord();  
  187. _recordingTime = 0;  
  188. //停止  
  189. _player.stop();  
  190. }  
  191. }  

基本测试了下,不知道还有啥问题,日后在改,通过做这个录音机,了解了FMS的基本使用流程,以及熟悉了netStream的强大...
http://hi.baidu.com/enqkmhzhofbeirq/item/fc924ec680be230fc710b2f2

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器