Android 如何使用AudioRecord录制音频并转换成wav格式
时间: 2023-12-19 07:04:05 浏览: 194
使用AudioRecord录制音频并转换成wav格式,需要进行以下步骤:
1. 设置录音参数:采样率、音频通道、编码格式等
2. 创建一个AudioRecord对象
3. 开始录制音频,将音频数据写入到一个缓存区
4. 录制完成后,停止录音并释放AudioRecord对象
5. 将缓存区中的音频数据写入到一个wav文件中
下面是一个简单的示例代码,演示如何使用AudioRecord录制音频并将其转换成wav格式:
```
// 设置录音参数
int sampleRateInHz = 44100;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
// 创建AudioRecord对象
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
// 开始录音
audioRecord.startRecording();
// 定义缓存区
byte[] buffer = new byte[bufferSizeInBytes];
// 定义输出文件
String outputFileName = "output.wav";
File outputFile = new File(Environment.getExternalStorageDirectory(), outputFileName);
// 定义输出流
FileOutputStream outputStream = new FileOutputStream(outputFile);
// 写入wav文件头
WaveHeader waveHeader = new WaveHeader(sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
waveHeader.write(outputStream);
// 写入音频数据
int readSize;
while ((readSize = audioRecord.read(buffer, 0, bufferSizeInBytes)) != AudioRecord.ERROR_INVALID_OPERATION) {
outputStream.write(buffer, 0, readSize);
}
// 停止录音并释放资源
audioRecord.stop();
audioRecord.release();
// 关闭输出流
outputStream.close();
```
在上述代码中,我们使用了一个自定义的WaveHeader类,用于生成wav文件头信息。该类的实现可以参考下面的示例代码:
```
public class WaveHeader {
private int sampleRate;
private int channelCount;
private int audioFormat;
private int audioDataLength;
public WaveHeader(int sampleRate, int channelCount, int audioFormat, int audioDataLength) {
this.sampleRate = sampleRate;
this.channelCount = channelCount;
this.audioFormat = audioFormat;
this.audioDataLength = audioDataLength;
}
public void write(OutputStream outputStream) throws IOException {
outputStream.write("RIFF".getBytes());
outputStream.write(intToByteArray(36 + audioDataLength), 0, 4);
outputStream.write("WAVE".getBytes());
outputStream.write("fmt ".getBytes());
outputStream.write(intToByteArray(16), 0, 4);
outputStream.write(shortToByteArray((short) 1), 0, 2);
outputStream.write(shortToByteArray((short) channelCount), 0, 2);
outputStream.write(intToByteArray(sampleRate), 0, 4);
outputStream.write(intToByteArray(sampleRate * channelCount * audioFormat / 8), 0, 4);
outputStream.write(shortToByteArray((short) (channelCount * audioFormat / 8)), 0, 2);
outputStream.write(shortToByteArray((short) audioFormat), 0, 2);
outputStream.write("data".getBytes());
outputStream.write(intToByteArray(audioDataLength), 0, 4);
}
private byte[] intToByteArray(int value) {
byte[] byteArray = new byte[4];
byteArray[0] = (byte) (value & 0xff);
byteArray[1] = (byte) ((value >> 8) & 0xff);
byteArray[2] = (byte) ((value >> 16) & 0xff);
byteArray[3] = (byte) ((value >> 24) & 0xff);
return byteArray;
}
private byte[] shortToByteArray(short value) {
byte[] byteArray = new byte[2];
byteArray[0] = (byte) (value & 0xff);
byteArray[1] = (byte) ((value >> 8) & 0xff);
return byteArray;
}
}
```
通过以上代码,我们可以实现将AudioRecord录制的音频转换成wav格式并保存到文件中。需要注意的是,由于Android 6.0及以上版本需要动态获取录音权限,因此在使用前需要先请求录音权限。
阅读全文