AudioRecord实现"助听器"

来源:互联网 发布:非农就业数据原因 编辑:程序博客网 时间:2024/05/16 01:49

Android可以通过MediaRecorder和AudioRecord这两个工具来实现录音,MediaRecorder直接把麦克风的数据存到文件,并且能够直接进行编码(如AMR,MP3等),而AudioRecord则是读取麦克风的音频流。本文使用AudioRecord读取音频流,使用AudioTrack播放音频流,通过“边读边播放”以及增大音量的方式来实现一个简单的助听器程序。

PS:由于目前的Android模拟器还不支持AudioRecord,因此本程序需要编译之后放到真机运行。

先贴出本文程序运行截图:


PS:程序音量调节只是程序内部调节音量而已,要调到最大音量还需要手动设置系统音量。


使用AudioRecord必须要申请许可,在AndroidManifest.xml里面添加这句:

main.xml的源码如下:


  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.         android:orientation="vertical" android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent">

  4.         <button android:layout_height="wrap_content" android:id="@+id/btnRecord" 
  5.                 android:layout_width="fill_parent" android:text="开始边录边放">
  6.         <button android:layout_height="wrap_content" 
  7.                 android:layout_width="fill_parent" android:text="停止" android:id="@+id/btnStop">
  8.         <button android:layout_height="wrap_content" android:id="@+id/btnExit" 
  9.                 android:layout_width="fill_parent" android:text="退出">
  10.         <textview android:id="@+id/TextView01" android:layout_height="wrap_content" 
  11.                 android:text="程序音量调节" android:layout_width="fill_parent">
  12.         <seekbar android:layout_height="wrap_content" android:id="@+id/skbVolume" 
  13.                 android:layout_width="fill_parent">


复制代码
testRecord.java的源码如下:

  1. package com.testRecord;

  2. import android.app.Activity;
  3. import android.media.AudioFormat;
  4. import android.media.AudioManager;
  5. import android.media.AudioRecord;
  6. import android.media.AudioTrack;
  7. import android.media.MediaRecorder;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.SeekBar;
  12. import android.widget.Toast;

  13. public class testRecord extends Activity {
  14.         /** Called when the activity is first created. */
  15.         Button btnRecord, btnStop, btnExit;
  16.         SeekBar skbVolume;//调节音量
  17.         boolean isRecording = false;//是否录放的标记
  18.         static final int frequency = 44100;
  19.         static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
  20.         static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
  21.         int recBufSize,playBufSize;
  22.         AudioRecord audioRecord;
  23.         AudioTrack audioTrack;

  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                 setContentView(R.layout.main);
  28.                 setTitle("助听器");
  29.                 recBufSize = AudioRecord.getMinBufferSize(frequency,
  30.                                 channelConfiguration, audioEncoding);

  31.                 playBufSize=AudioTrack.getMinBufferSize(frequency,
  32.                                 channelConfiguration, audioEncoding);
  33.                 // -----------------------------------------
  34.                 audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
  35.                                 channelConfiguration, audioEncoding, recBufSize);

  36.                 audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
  37.                                 channelConfiguration, audioEncoding,
  38.                                 playBufSize, AudioTrack.MODE_STREAM);
  39.                 //------------------------------------------
  40.                 btnRecord = (Button) this.findViewById(R.id.btnRecord);
  41.                 btnRecord.setOnClickListener(new ClickEvent());
  42.                 btnStop = (Button) this.findViewById(R.id.btnStop);
  43.                 btnStop.setOnClickListener(new ClickEvent());
  44.                 btnExit = (Button) this.findViewById(R.id.btnExit);
  45.                 btnExit.setOnClickListener(new ClickEvent());
  46.                 skbVolume=(SeekBar)this.findViewById(R.id.skbVolume);
  47.                 skbVolume.setMax(100);//音量调节的极限
  48.                 skbVolume.setProgress(70);//设置seekbar的位置值
  49.                 audioTrack.setStereoVolume(0.7f, 0.7f);//设置当前音量大小
  50.                 skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  51.                         
  52.                         @Override
  53.                         public void onStopTrackingTouch(SeekBar seekBar) {
  54.                                 float vol=(float)(seekBar.getProgress())/(float)(seekBar.getMax());
  55.                                 audioTrack.setStereoVolume(vol, vol);//设置音量
  56.                         }
  57.                         
  58.                         @Override
  59.                         public void onStartTrackingTouch(SeekBar seekBar) {
  60.                                 // TODO Auto-generated method stub
  61.                         }
  62.                         
  63.                         @Override
  64.                         public void onProgressChanged(SeekBar seekBar, int progress,
  65.                                         boolean fromUser) {
  66.                                 // TODO Auto-generated method stub
  67.                         }
  68.                 });
  69.         }

  70.         @Override
  71.         protected void onDestroy() {
  72.                 super.onDestroy();
  73.                 android.os.Process.killProcess(android.os.Process.myPid());
  74.         }

  75.         class ClickEvent implements View.OnClickListener {

  76.                 @Override
  77.                 public void onClick(View v) {
  78.                         if (v == btnRecord) {
  79.                                 isRecording = true;
  80.                                 new RecordPlayThread().start();// 开一条线程边录边放
  81.                         } else if (v == btnStop) {
  82.                                 isRecording = false;
  83.                         } else if (v == btnExit) {
  84.                                 isRecording = false;
  85.                                 testRecord.this.finish();
  86.                         }
  87.                 }
  88.         }

  89.         class RecordPlayThread extends Thread {
  90.                 public void run() {
  91.                         try {
  92.                                 byte[] buffer = new byte[recBufSize];
  93.                                 audioRecord.startRecording();//开始录制
  94.                                 audioTrack.play();//开始播放
  95.                                 
  96.                                 while (isRecording) {
  97.                                         //从MIC保存数据到缓冲区
  98.                                         int bufferReadResult = audioRecord.read(buffer, 0,
  99.                                                         recBufSize);

  100.                                         byte[] tmpBuf = new byte[bufferReadResult];
  101.                                         System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
  102.                                         //写入数据即播放
  103.                                         audioTrack.write(tmpBuf, 0, tmpBuf.length);
  104.                                 }
  105.                                 audioTrack.stop();
  106.                                 audioRecord.stop();
  107.                         } catch (Throwable t) {
  108.                                 Toast.makeText(testRecord.this, t.getMessage(), 1000);
  109.                         }
  110.                 }
  111.         };
  112. }
0 0