Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 進階:用 AudioRecord 與 AudioTrack 做一個助聽器

編輯:關於 Android 程式設計

Android 上做錄音,一般會想到 MediaRecorder 和 AudioRecord 這兩條路。前者把麥克風資料直接寫成檔案,還能順便編碼成 AMR、MP3 等格式;後者則是拿到原始的 PCM 音頻流,資料就在手上,想怎麼處理都行。

本文走的是第二條路:用 AudioRecord 讀音頻流,再用 AudioTrack 把它播出去,配合音量增益,就是一個最樸素的助聽器——邊收邊放,順便把聲音放大。

動手之前有兩件事要先知道。第一,當時的 Android 模擬器並不支援 AudioRecord,所以這個程式編譯完必須放到真機上跑。第二,程式內的滑桿只能調整應用自身的音量,想要真正的最大聲,还得 manually 去把系統音量也推到頂。

使用 AudioRecord 必須先申請權限,在 AndroidManifest.xml 裡加上:

 <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> 

main.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_height="wrap_content" android:id="@+id/btnRecord" android:layout_width="fill_parent" android:text="開始邊錄邊放"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="停止" android:id="@+id/btnStop"></Button> <Button android:layout_height="wrap_content" android:id="@+id/btnExit" android:layout_width="fill_parent" android:text="退出"></Button> <TextView android:id="@+id/TextView01" android:layout_height="wrap_content" android:text="程序音量調節" android:layout_width="fill_parent"></TextView> <SeekBar android:layout_height="wrap_content" android:id="@+id/skbVolume" android:layout_width="fill_parent"></SeekBar> </LinearLayout> 

testRecord.java:

 package com.testRecord; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioRecord; import android.media.AudioTrack; import android.media.MediaRecorder; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.Toast; public class testRecord extends Activity { Button btnRecord, btnStop, btnExit; SeekBar skbVolume;//調節音量 boolean isRecording = false;//是否錄放的標記 static final int frequency = 44100; static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO; static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; int recBufSize,playBufSize; AudioRecord audioRecord; AudioTrack audioTrack; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("助聽器"); recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding); playBufSize=AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding); // ----------------------------------------- audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, recBufSize); audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, playBufSize, AudioTrack.MODE_STREAM); //------------------------------------------ btnRecord = (Button) this.findViewById(R.id.btnRecord); btnRecord.setOnClickListener(new ClickEvent()); btnStop = (Button) this.findViewById(R.id.btnStop); btnStop.setOnClickListener(new ClickEvent()); btnExit = (Button) this.findViewById(R.id.btnExit); btnExit.setOnClickListener(new ClickEvent()); skbVolume=(SeekBar)this.findViewById(R.id.skbVolume); skbVolume.setMax(100);//音量調節的極限 skbVolume.setProgress(70);//設置seekbar的位置值 audioTrack.setStereoVolume(0.7f, 0.7f);//設置當前音量大小 skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { float vol=(float)(seekBar.getProgress())/(float)(seekBar.getMax()); audioTrack.setStereoVolume(vol, vol);//設置音量 } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); } @Override protected void onDestroy() { super.onDestroy(); android.os.Process.killProcess(android.os.Process.myPid()); } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btnRecord) { isRecording = true; new RecordPlayThread().start();// 開一條線程邊錄邊放 } else if (v == btnStop) { isRecording = false; } else if (v == btnExit) { isRecording = false; testRecord.this.finish(); } } } class RecordPlayThread extends Thread { public void run() { try { byte[] buffer = new byte[recBufSize]; audioRecord.startRecording();//開始錄制 audioTrack.play();//開始播放 while (isRecording) { //從MIC保存數據到緩沖區 int bufferReadResult = audioRecord.read(buffer, 0, recBufSize); byte[] tmpBuf = new byte[bufferReadResult]; System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult); //寫入數據即播放 audioTrack.write(tmpBuf, 0, tmpBuf.length); } audioTrack.stop(); audioRecord.stop(); } catch (Throwable t) { Toast.makeText(testRecord.this, t.getMessage(), 1000); } } }; }

這個例子示範了「讀—寫」串接最直觀的樣子。真實場景裡當然還可以插入濾波、降噪或增益演算法,骨架卻是一樣的。希望對大家的 Android 音頻開發有參考價值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved