当前位置: > > > Flex手机项目 - 调用麦克风录音并播放

Flex手机项目 - 调用麦克风录音并播放

在AS3的移动项目中,使用Microphone类能通过设备的麦克风来记录音频流。

下面是一个使用麦克风录音并播放的demo,效果图如下:


代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
					   xmlns:s="library://ns.adobe.com/flex/spark"
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   title="麦克风测试"
					   creationComplete="onCreationComplete()">

	<fx:Script>  
		<![CDATA[ 
			import flash.events.SampleDataEvent;
			import flash.media.Microphone;
			import flash.media.Sound;
			import flash.utils.ByteArray;
			
			import mx.collections.ArrayCollection; 
			
			private var microphone:Microphone; 
			private var isRecording:Boolean = false; 
			private var soundByteArray:ByteArray; 
			private var soundObj:Sound; 

			/**
			 * 页面初始化
			 */
			private function onCreationComplete():void 
			{ 
				//加载麦克风列表
				soundCaptureDevices.dataProvider = new ArrayCollection(Microphone.names);
				soundCaptureDevices.selectedIndex = 0;
			} 
			
			/**
			 * 开始录音
			 */
			private function startRecording():void 
			{ 
				startBtn.enabled = false;
				stopBtn.enabled = true;
				playBtn.enabled = false;
				
				soundByteArray = new ByteArray();
				
				microphone = Microphone.getMicrophone(soundCaptureDevices.selectedIndex);
				microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData); 
				microphone.rate = 44; //麦克风捕获声音采样速率
				microphone.gain = 100; //麦克风在传输音频信号之前需要倍增音量
				//设置应该考虑为声音的最小输入音量水平,以及静默实际开始之前的静音时间量
				microphone.setSilenceLevel(0, 1000); 
			} 
			
			/**
			 * 停止录音
			 */
			private function stopRecording():void 
			{ 
				startBtn.enabled = true;
				stopBtn.enabled = false;
				playBtn.enabled = true;
				
				microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData); 
			} 
			
			/**
			 * 录音中-录音数据持续接收响应
			 */
			private function onSampleData(micData:SampleDataEvent):void 
			{ 
				//持续记录音频流
				soundByteArray.writeBytes(micData.data); 
			} 
			
			/**
			 * 开始播放录音
			 */
			private function playRecording():void 
			{ 
				var trans:SoundTransform = new SoundTransform(1, -1); 
				startBtn.enabled = false;
				soundByteArray.position = 0;
				soundObj = new Sound(); 
				soundObj.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound); 
				soundObj.play(0, 1, trans); 
			} 
			
			/**
			 * 录音播放中
			 */
			private function playSound(e:SampleDataEvent):void 
			{
				if (!soundByteArray.bytesAvailable > 0) 
				{
					startBtn.enabled = true;
					stopBtn.enabled = false;
					playBtn.enabled = false;
					return; 
				}
				
				for (var i:int = 0; i < 8192; i++) 
				{ 
					var audioSample:Number = 0; 
					
					if (soundByteArray.bytesAvailable > 0) 
					{
						audioSample = soundByteArray.readFloat(); 
					}
					
					//调用writeFloat()方法两次是为了音频数据采样能同时命中左声道和右声道
					e.data.writeFloat(audioSample); 
					e.data.writeFloat(audioSample); 
				}
			}
			
		]]> 
	</fx:Script> 
	
	<s:layout>
		<s:VerticalLayout paddingLeft="20" paddingRight="20" paddingBottom="20" paddingTop="20"/>
	</s:layout>

	<s:VGroup width="437" gap="20" horizontalAlign="center">
		<s:Label id="description" text="选择一个麦克风并开始录音..." height="30"/>
		
		<s:ComboBox id="soundCaptureDevices" width="397" height="55" 
					textAlign="center" focusEnabled="false"/>
		
		<s:HGroup width="437" gap="20" horizontalAlign="center">
		
			<s:Button id="startBtn" label="录音" chromeColor="#51B22F" click="startRecording()"/>  
			
			<s:Button id="stopBtn" label="停止" chromeColor="#CB0909" click="stopRecording()"
					  enabled="false"/>  
			
			<s:Button id="playBtn" label="播放" click="playRecording()" enabled="false"/>
			
		</s:HGroup>
			
	</s:VGroup>
</s:View>
评论1
  • 1楼
    2015-05-07 01:11
    立月柒

    谢谢分享 可以用

    站长回复

    不客气的。