【发布时间】:2011-07-16 01:29:44
【问题描述】:
我编写了一个简单的 Android 代码来从我的 HTC HD Desire Android Mobile 捕获音频,并将其写入我 SD 卡上的音频文件。我的代码编写了音频文件,但我无法打开该文件。我怀疑音频文件格式不对。
这是我编写的代码,并解释了它是如何工作的。我感谢有关如何调试我的代码的任何提示。谢谢。
代码
记录器类有一个方法,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 驱动录音机代码。该类使用一个简单的 FileOutputStream 封装在 BufferedOUTputStream 中,并将录音机中的音频缓冲区数组写入文件。
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>
【问题讨论】: