我编写了一个简单的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驱动录音机代码。此类使用包装在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 list :
<?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>
最佳答案
AudioRecord产生的数据由PCM样本组成。这是声音的“原始”形式。在上面的示例中,您每秒将获得8,000个16位(两个字节)样本。将此数据写入文件只会使用此原始数据创建一个文件。
您可能想要的是数据采用某种标准文件格式,该格式具有某种 header 和可能的压缩(也称为编码)。
以已知格式记录音频的最简单方法是使用MediaRecorder类。 MediaRecorder的文档有一个简短的示例,在这里我不再赘述。
关于java - 如何从我的Android手机麦克风捕获音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6714677/