我如何从蓝牙耳机接收音频?

时间:2018-02-27 20:32:21

标签: java android bluetooth

我正在尝试通过蓝牙耳机接收声音,但这在Android中不起作用?和权限已添加到清单

这是我的代码

            init();
            recordAndPlay();


}
private void init() {
    int min = AudioRecord.getMinBufferSize(sampleRateHZ , AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    record = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateHZ , AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, min);



    int maxJitter = AudioTrack.getMinBufferSize(sampleRateHZ , AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
    track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, sampleRateHZ , AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);



}

private void recordAndPlay() {
    short[] Buffer = new short[5000];
    int num = 0;
    am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    am.startBluetoothSco();
    am.setBluetoothScoOn(true);

    am.setMode(AudioManager.MODE_NORMAL);
    record.startRecording();
    track.play();
    while (true) {
        num = record.read(Buffer, 0, 5000);
        track.write(Buffer, 0, num);
    }
}

0 个答案:

没有答案