本文介绍的 是录音采用AudioRecorder 然后 保存PCM 格式到文件 然后 使用AudioTrack 播放PCM 同时 支持 蓝牙耳机录制 音频
下面贴下代码
1 AudioRecorder 录制和播放
package com.kwai.video.uikit.bluetoothapplication;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.text.TextUtils;
import android.util.Log;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class AudioRecordManager {
public static final String TAG = "AudioRecordManager";
private AudioRecord mRecorder;
private DataOutputStream dos;
private Thread recordThread;
private boolean isStart = false;
private static AudioRecordManager mInstance;
private int bufferSize;
public AudioRecordManager() {
//CHANNEL_IN_STEREO 双声道 输入
//ENCODING_PCM_16BIT 16 bit 字节
bufferSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2);
}
/**
* 获取单例引用
*
* @return
*/
public static AudioRecordManager getInstance() {
if (mInstance == null) {
synchronized (AudioRecordManager.class) {
if (mInstance == null) {
mInstance = new AudioRecordManager();
}
}
}
return mInstance;
}
/**
* 销毁线程方法
*/
private void destroyThread() {
try {
isStart = false;
if (null != recordThread && Thread.State.RUNNABLE == recordThread.getState()) {
try {
Thread.sleep(500);
recordThread.interrupt();
} catch (Exception e) {
recordThread = null;
}

本文详细介绍了如何在Android平台上使用AudioRecord进行PCM格式的录音,并通过AudioTrack实现播放。同时,文章还展示了如何启用蓝牙耳机进行音频录制,包括开启和关闭SCO连接的实现。整个过程涵盖了从初始化、录音、播放到错误处理的完整流程。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



