感謝灰色飄零大佬的這篇指南html
Android提供了兩種音頻錄製方式, 這裏講比較底層一些的AudioRecord.java
引用一下google的定義:緩存
AudioRecord類管理Java程序的音頻資源, 以記錄來自音頻輸入硬件的音頻. 這是經過向AudioRecord對象讀取數據來實現的. 應用經過如下三種方法之一及時的查詢AudioRecord對象:ide
- read(byte[], int, int)
- read(short[], int, int)
- read(ByteBuffer, int)
具體使用哪一個, 是根據音頻數據存儲格式自由選擇的. 建立後, AudioRecord對象初始化其關聯的音頻緩衝區, 它將填充新的音頻數據. 在構造期間指定的緩衝區大小決定了AudioRecord在"過分運行"時, 還沒有讀取的數據能夠保存多長時間. 所以從音頻硬件讀取的數據塊大小應該小於緩衝區大小.工具
下面是具體實現ui
private void checkPermission() {
//6.0之後須要動態申請權限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//檢查是否已經獲取權限, 沒有獲取的權限放到list裏
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permission);
}
}
//list不爲空, 就去申請所須要的權限
if (!mPermissionList.isEmpty()) {
String[] permissions = mPermissionList.toArray(new String[0]);
ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}
}
}
複製代碼
這裏寫了個方法來建立AudioRecord:this
private void createAudioRecord() {
//這裏經過AudioRecord類獲取最小的緩存大小
recordBufSize =
AudioRecord.getMinBufferSize(GlobalConfig.SAMPLE_RATE_INHZ, GlobalConfig.CHANNEL_CONFIG,
GlobalConfig.AUDIO_FORMAT);
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, GlobalConfig.SAMPLE_RATE_INHZ,
GlobalConfig.CHANNEL_CONFIG, GlobalConfig.AUDIO_FORMAT, recordBufSize);
}
複製代碼
用到的配置類:google
public class GlobalConfig {
//採樣率
public static final int SAMPLE_RATE_INHZ = 44100;
//聲道
public static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
//返回的音頻數據格式
public static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
}
複製代碼
private void startRecord() {
createAudioRecord();
//buffer, 用來從AudioRecord中獲取錄製的音頻
final byte[] data = new byte[recordBufSize];
//pcm文件存儲位置
final File file = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), "test.pcm");
if (!file.mkdirs()) {
Log.d(TAG, "startRecord: " + "Directory not created");
}
if (file.exists()) {
file.delete();
}
//開始錄製
mAudioRecord.startRecording();
//錄製標記
isRecording = true;
//不阻塞主線程
new Thread(new Runnable() {
@Override public void run() {
//新建文件輸出流, 把錄製音頻存儲
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fileOutputStream != null) {
while (isRecording) {
//將錄製音頻傳到buffer裏
int read = mAudioRecord.read(data, 0, recordBufSize);
if (read != AudioRecord.ERROR_INVALID_OPERATION) {
try {
//buffer數據沒問題寫入到文件中
fileOutputStream.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
//不要忘記關文件輸出流
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
複製代碼
private void stopRecord() {
//中止錄音
isRecording = false;
//不要泄露
if (mAudioRecord != null) {
mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
}
}
複製代碼
這裏直接給一個大佬提供的轉化工具類:spa
/** * 將pcm音頻文件轉換爲wav音頻文件 */
public class PcmToWavUtil {
/** * 緩存的音頻大小 */
private int mBufferSize;
/** * 採樣率 */
private int mSampleRate;
/** * 聲道數 */
private int mChannel;
/** * @param sampleRate sample rate、採樣率 * @param channel channel、聲道 * @param encoding Audio data format、音頻格式 */
PcmToWavUtil(int sampleRate, int channel, int encoding) {
this.mSampleRate = sampleRate;
this.mChannel = channel;
this.mBufferSize = AudioRecord.getMinBufferSize(mSampleRate, mChannel, encoding);
}
/** * pcm文件轉wav文件 * * @param inFilename 源文件路徑 * @param outFilename 目標文件路徑 */
public void pcmToWav(String inFilename, String outFilename) {
FileInputStream in;
FileOutputStream out;
long totalAudioLen;
long totalDataLen;
long longSampleRate = mSampleRate;
int channels = mChannel == AudioFormat.CHANNEL_IN_MONO ? 1 : 2;
long byteRate = 16 * mSampleRate * channels / 8;
byte[] data = new byte[mBufferSize];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
writeWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
while (in.read(data) != -1) {
out.write(data);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** * 加入wav文件頭 */
private void writeWaveFileHeader(FileOutputStream out, long totalAudioLen, long totalDataLen, long longSampleRate, int channels, long byteRate) throws IOException {
byte[] header = new byte[44];
// RIFF/WAVE header
header[0] = 'R';
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);
//WAVE
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
// 'fmt ' chunk
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
// 4 bytes: size of 'fmt ' chunk
header[16] = 16;
header[17] = 0;
header[18] = 0;
header[19] = 0;
// format = 1
header[20] = 1;
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 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);
// block align
header[32] = (byte) (2 * 16 / 8);
header[33] = 0;
// bits per sample
header[34] = 16;
header[35] = 0;
//data
header[36] = 'd';
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);
out.write(header, 0, 44);
}
}
複製代碼
到這裏, 就能夠進行音頻錄製啦線程