我編寫了一個簡單的Android代碼,用於從我的HTC HD Desire Android Mobile中捕獲音頻,並將其粘貼到SD卡上的音頻文件中。我的代碼寫了音頻文件,但我無法打開文件。我懷疑音頻文件格式不正確。如何從我的Android手機麥克風中捕獲音頻
這是我編寫的代碼,並解釋了它的工作原理。我很感激關於如何調試我的代碼的任何提示。謝謝。
代碼
記錄器類具有一個方法,的StartRecording(),即負責從麥克風捕獲聲音20秒。
package com.audio;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
public class Recorder {
private short[] recordedAudioBuffer;
private int bufferRead;
public short[] startRecording() {
int recorderBufferSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT) * 2;
AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
recorderBufferSize);
recordedAudioBuffer = new short[recorderBufferSize];
recorder.startRecording();
bufferRead = recorder.read(recordedAudioBuffer, 0, recorderBufferSize);
synchronized (this) {
try {
this.wait(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
recorder.stop();
recorder.release();
return recordedAudioBuffer;
}
public int getBufferRead() {
return bufferRead;
}
}
我用AudioRecorderActivity驅動錄音機代碼。此類使用包裝在BufferedOUtputStream中的簡單FileOutputStream,並將記錄器中的音頻緩衝區數組寫入文件。
package com.audio;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
public class AudioRecorderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create a file to dump the recording
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/test.raw");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Record audio
Recorder recorderInstance = new Recorder();
short[] recordedAudioBuffer = recorderInstance.startRecording();
// Write the audio to the file
BufferedOutputStream bufferedStreamInstance = null;
try {
bufferedStreamInstance = new BufferedOutputStream(
new FileOutputStream(f));
} catch (FileNotFoundException e) {
throw new IllegalStateException("Cannot Open File", e);
}
DataOutputStream dataOutputStreamInstance = new DataOutputStream(
bufferedStreamInstance);
try {
for (int idxBuffer = 0; idxBuffer < recordedAudioBuffer.length; idxBuffer++) {
dataOutputStreamInstance
.writeShort(recordedAudioBuffer[idxBuffer]);
}
} catch (IOException e) {
throw new IllegalStateException(
"dataOutputStreamInstance.writeShort(curVal)");
}
}
}
,這是我的Android清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.audio"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AudioRecorderActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>