Android 蓝牙耳机录音以及蓝牙耳机播放


此文是借鉴于CSDN 某大牛博客改进之后的代码

首先, 要保证蓝牙设备的链接,可用手机直接蓝牙设备进行连接,连接的代码不写了
如蓝牙耳机已开启则直接运行以下步骤
1.  实例化  录音对象  
  Recorder mRecorder = new Recorder(mContext);
1.1开始录音
mRecorder.startRecord(new Recorder.RecoderListener() {
    @Override
    public void onData(byte[] data) {  //data 为录音的原始数据  pcm格式原始数据。根据个人需要进行转化。
       }
 }
1.2停止录音
mRecorder.stopRecord();

2.蓝牙耳机播放 

2.1蓝牙耳机播放对象的初始化

AudioManager mAudioManager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);

int audioBufSize = AudioTrack.getMinBufferSize(16000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);

AudioTrack player = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 16000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, audioBufSize,AudioTrack.MODE_STREAM);

2.2 播放

首先将要播放的数据写入到设备当中 写入之后调用播放 API。

    player.write(data, index, length);   // 数据写入蓝牙    data  为播放是数据源。  index 为播放起点。 length 数据长度

  player.play(); //播放
录音实现代码 可直接粘贴走
public class Recorder {
    private Context mContext;
    private AudioManager mAudioManager;
    private RecoderListener mListener;
    private RecordThread mRecordingThread = null;

    public Recorder(Context context) {
        this.mContext = context;
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    }

    public interface RecoderListener {
        void onData(byte[] data);
    }

    public void startRecord(RecoderListener listener) {
        mListener = listener;
        if (mAudioManager.isBluetoothScoAvailableOffCall()) {
            if (mAudioManager.isBluetoothScoOn()) {
                mAudioManager.stopBluetoothSco();
                Log.e("BTRecordImpl", "1mAudioManager.stopBluetoothSco()");
            }
            Log.e("BTRecordImpl", "1-->startBluetoothSco");
            mAudioManager.startBluetoothSco();
            long timeout = 100;

            while (!mAudioManager.isBluetoothScoOn() && timeout-- > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (timeout == 50) {
                    Log.e("BTRecordImpl", "2-->startBluetoothSco");
                    mAudioManager.startBluetoothSco();
                }
                Log.e("BTRecordImpl", "change BluetoothScoOn  -->" + mAudioManager.isBluetoothScoOn() + ":" + timeout);
            }
            if (mRecordingThread != null) {
                mRecordingThread.pause();
                mRecordingThread.interrupt();
            }
            mRecordingThread = new RecordThread();
            mRecordingThread.start();
        }
    }


    private class RecordThread extends Thread {
        private AudioRecord audioRecord;
        private int bufferSize;
        private boolean isRun = false;
        private long mStartTime = 0L;
        private int SAMPLE_RATE_HZ = 16000;

        public RecordThread() {
            int audiosource = MediaRecorder.AudioSource.VOICE_RECOGNITION;

            if (Build.VERSION.SDK_INT > 19) {
                audiosource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
            }
            this.bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE_HZ, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT) * 2;
            this.audioRecord = new AudioRecord(audiosource, SAMPLE_RATE_HZ, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, this.bufferSize);
        }

        @Override
        public void run() {
            super.run();
            this.isRun = true;
            try {
                if (audioRecord.getState() == 1) {
                    this.audioRecord.startRecording();
                    mStartTime = System.currentTimeMillis();
                    while (this.isRun) {
                        byte[] buffer = new byte[bufferSize];
                        int readBytes = audioRecord.read(buffer, 0, bufferSize);
                        if (readBytes > 0) {
                            if (mListener != null) {
                                mListener.onData(buffer);
                            }
                        }
                    }
                    try {
                        this.audioRecord.stop();
                        this.audioRecord.release();
                    } catch (Exception audioException) {

                    }
                    Log.e("RecordingManager", "endVoiceRequest() --> ");
                }
            } catch (Exception e2) {
                Log.e("BtRecordImpl", "error: " + e2.getMessage());
                try {
                    this.audioRecord.stop();
                    this.audioRecord.release();
                } catch (Exception audioException) {

                }
                isRun = false;
            }
        }

        void pause() {
            this.isRun = false;
            try {
                this.audioRecord.stop();
                this.audioRecord.release();
            } catch (Exception e) {
            }
        }

        public synchronized void start() {
            if (!isRun) {
                super.start();
            }
        }
    }
//停止录音
    public void stopRecord() {
        System.out.println("stopRecord");
        if (mRecordingThread != null) {
            mRecordingThread.pause();
            mRecordingThread = null;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/langxian_168/article/details/89358483