@[TOC](录制音频后 播放速度变快)
录制音频文件
我使用aduiorecord录制pcm文件 录制代码如下
创建实例
mAudioRecorder = new AudioRecord(
mRecorderBuilder.mAudioSource,
sample_rate,
channel_config,
format,
mRecorderBuilder.bufferSize);
其中参数含义
private int mAudioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;//自动降噪,也尝试过 MIC
private int mSampleRate = SAMPLE_RATE_44K_HZ;//44100
private int mChannelConfig = AudioFormat.CHANNEL_IN_MONO;//单声道
private int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;//每次采样位数
mRecorderBuilder.bufferSize//缓冲区大小,其值大于 用AudioRecord.getMinBufferSize(sample_rate, channel_config, format) 获取的最小值
录音
//mPcmBuffer.size就是上面的mRecorderBuilder.bufferSize 大小
//private short[] mPcmBuffer
mAudioRecorder.read(mPcmBuffer, 0, mPcmBuffer.length);
存储
我是通过随机存储文件对象,进行数据的存储.
由于需要存储为wav格式的文件,因此需要写头文件.
头文件中需要计算 当前录音的格式 数据
第一次写,网上找的格式写的[1]
RandomAccessFile rand = new RandomAccessFile(saveFile, "rw");
RandomAccessFile rand = randomAccessFile;
long totalDataLen = totalAudioLen + 36;
rand.seek(0);
byte[] header = new byte[44];
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte) (nChannels & 0xff);
header[23] = (byte) ((nChannels >> 8) & 0xff);
header[24] = (byte) (sampleRate & 0xff);//采样率
header[25] = (byte) ((sampleRate >> 8) & 0xff);
header[26] = (byte) ((sampleRate >> 16) & 0xff);
header[27] = (byte) ((sampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);//取八位
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
int b = weikuan * nChannels / 8;//每次采样的大小
header[32] = (byte) (b & 0xff); // block align
header[33] = (byte) ((b >> 8) & 0xff);
header[34] = (byte) (weikuan & 0xff);
header[35] = (byte) ((weikuan >> 8) & 0xff);
header[36] = 'd';//data
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
rand.write(header, 0, 44);
写完之后将写入pcm数据流 我这里分装了一个对象,单独开了一个写文件线程.
通过handler 开启工作线程(之前的录音也是一个单独的线程具体看demo)
saveFileThread = new SaveFileThread();
saveFileThread.start();
saveHandler = new SaveFileThread.SaveHandler(saveFileThread);
if (msg.what == SAVEING) {
saving = true;
while (saveFileThread.doWork() > 0) ;
// Log.e(SaveFileThreadTAG, "收到 msg");
saving = false;
} else if (msg.what == STOP) {
saveFileThread.finish();
removeCallbacksAndMessages(null);
getLooper().quit();
}
SaveTask remove = saveDatas.remove(0);
int mSize = remove.mSize;
saveFile.saveByet(remove.saveData, 0, mSize);
至此wav文件录制完毕
遇见的问题
录出来的wav文件,播放时 语速非常快, 感觉是快进了两倍.
找了很多原因都找不到,网上搜了几天,无果.只有一篇帖子很像[2]
但是该帖子并没有解决我的问题
特此发帖,一方面是记录一下自己的demo
二来,希望哪位大神可以帮我看一下,我的demo到底出了说明问题
ps :demo中还实现了 lame库 将 pcm格式转换为mp3 格式的文件.使用 pcm格式文件转换可以正常使用
我的demo地址 https://github.com/MartinLi89/myrecorder.git
-
wav头文件说明(https://blog.csdn.net/hsy12342611/article/details/80075836) ↩
-
帖子https://blog.csdn.net/marller/article/details/52882387 ↩