我写了一个简单的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)");
}
}
}
这是我的机器人清单:
<?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>
答案 0 :(得分:0)
AudioRecord生成的数据由PCM样本组成。这是声音的“原始”形式。在上面的示例中,您将获得每秒8,000个16位(两个字节)的样本。将此数据写入文件只会创建包含此原始数据的文件。
你可能想要的是数据是某种标准文件格式,它有某种标题,可能还有一些压缩(也就是编码)。
以已知格式录制音频的最简单方法是使用MediaRecorder类。 MediaRecorder的文档有一个简短的样本,我不会在这里重复。
答案 1 :(得分:0)
如果您不想在应用上实现此功能,也可以使用意图
final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
File audioFile = new File(Environment.getExternalStorageDirectory(), "videoTest.mp4");
audioFileUri = Uri.fromFile(audioFile);
audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri);
startActivityForResult(audioIntent, TAKE_AUDIO);
audioFileUri是SD卡中文件的uri