本文来自http://blog.youkuaiyun.com/hellogv/,引用必须注明出处!
Android可以通过MediaRecorder和AudioRecord这两个工具来实现录音,MediaRecorder直接把麦克风的数据存到文件,并且能够直接进行编码(如AMR,MP3等),而AudioRecord则是读取麦克风的音频流。本文使用AudioRecord读取音频流,使用AudioTrack播放音频流,通过“边读边播放”以及增大音量的方式来实现一个简单的助听器程序。
PS:由于目前的Android模拟器还不支持AudioRecord,因此本程序需要编译之后放到真机运行。
先贴出本文程序运行截图:
PS:程序音量调节只是程序内部调节音量而已,要调到最大音量还需要手动设置系统音量。
使用AudioRecord必须要申请许可,在AndroidManifest.xml里面添加这句:
- <uses-permissionandroid:name="android.permission.RECORD_AUDIO"></uses-permission>
main.xml的源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Buttonandroid:layout_height="wrap_content"android:id="@+id/btnRecord"
- android:layout_width="fill_parent"android:text="开始边录边放"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="停止"android:id="@+id/btnStop"></Button>
- <Buttonandroid:layout_height="wrap_content"android:id="@+id/btnExit"
- android:layout_width="fill_parent"android:text="退出"></Button>
- <TextViewandroid:id="@+id/TextView01"android:layout_height="wrap_content"
- android:text="程序音量调节"android:layout_width="fill_parent"></TextView>
- <SeekBarandroid:layout_height="wrap_content"android:id="@+id/skbVolume"
- android:layout_width="fill_parent"></SeekBar>
- </LinearLayout>
testRecord.java的源码如下:
- packagecom.testRecord;
- importandroid.app.Activity;
- importandroid.media.AudioFormat;
- importandroid.media.AudioManager;
- importandroid.media.AudioRecord;
- importandroid.media.AudioTrack;
- importandroid.media.MediaRecorder;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.SeekBar;
- importandroid.widget.Toast;
- publicclasstestRecordextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- ButtonbtnRecord,btnStop,btnExit;
- SeekBarskbVolume;//调节音量
- booleanisRecording=false;//是否录放的标记
- staticfinalintfrequency=44100;
- staticfinalintchannelConfiguration=AudioFormat.CHANNEL_CONFIGURATION_MONO;
- staticfinalintaudioEncoding=AudioFormat.ENCODING_PCM_16BIT;
- intrecBufSize,playBufSize;
- AudioRecordaudioRecord;
- AudioTrackaudioTrack;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setTitle("助听器");
- recBufSize=AudioRecord.getMinBufferSize(frequency,
- channelConfiguration,audioEncoding);
- playBufSize=AudioTrack.getMinBufferSize(frequency,
- channelConfiguration,audioEncoding);
- //-----------------------------------------
- audioRecord=newAudioRecord(MediaRecorder.AudioSource.MIC,frequency,
- channelConfiguration,audioEncoding,recBufSize);
- audioTrack=newAudioTrack(AudioManager.STREAM_MUSIC,frequency,
- channelConfiguration,audioEncoding,
- playBufSize,AudioTrack.MODE_STREAM);
- //------------------------------------------
- btnRecord=(Button)this.findViewById(R.id.btnRecord);
- btnRecord.setOnClickListener(newClickEvent());
- btnStop=(Button)this.findViewById(R.id.btnStop);
- btnStop.setOnClickListener(newClickEvent());
- btnExit=(Button)this.findViewById(R.id.btnExit);
- btnExit.setOnClickListener(newClickEvent());
- skbVolume=(SeekBar)this.findViewById(R.id.skbVolume);
- skbVolume.setMax(100);//音量调节的极限
- skbVolume.setProgress(70);//设置seekbar的位置值
- audioTrack.setStereoVolume(0.7f,0.7f);//设置当前音量大小
- skbVolume.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener(){
- @Override
- publicvoidonStopTrackingTouch(SeekBarseekBar){
- floatvol=(float)(seekBar.getProgress())/(float)(seekBar.getMax());
- audioTrack.setStereoVolume(vol,vol);//设置音量
- }
- @Override
- publicvoidonStartTrackingTouch(SeekBarseekBar){
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidonProgressChanged(SeekBarseekBar,intprogress,
- booleanfromUser){
- //TODOAuto-generatedmethodstub
- }
- });
- }
- @Override
- protectedvoidonDestroy(){
- super.onDestroy();
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- classClickEventimplementsView.OnClickListener{
- @Override
- publicvoidonClick(Viewv){
- if(v==btnRecord){
- isRecording=true;
- newRecordPlayThread().start();//开一条线程边录边放
- }elseif(v==btnStop){
- isRecording=false;
- }elseif(v==btnExit){
- isRecording=false;
- testRecord.this.finish();
- }
- }
- }
- classRecordPlayThreadextendsThread{
- publicvoidrun(){
- try{
- byte[]buffer=newbyte[recBufSize];
- audioRecord.startRecording();//开始录制
- audioTrack.play();//开始播放
- while(isRecording){
- //从MIC保存数据到缓冲区
- intbufferReadResult=audioRecord.read(buffer,0,
- recBufSize);
- byte[]tmpBuf=newbyte[bufferReadResult];
- System.arraycopy(buffer,0,tmpBuf,0,bufferReadResult);
- //写入数据即播放
- audioTrack.write(tmpBuf,0,tmpBuf.length);
- }
- audioTrack.stop();
- audioRecord.stop();
- }catch(Throwablet){
- Toast.makeText(testRecord.this,t.getMessage(),1000);
- }
- }
- };
- }