3

我正在从蓝牙耳机的麦克风录制实时音频并将其播放到手机的扬声器上。

流式代码:

 int bufferSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        arec = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
        //STREAM_MUSIC - no sound
        //STREAM_ALARM - beep from phone's speaker
        //STREAM_RING - no sound
        //STREAM_NOTIFICATION - no sound
        //STREAM_SYSTEM - no sound
        //STREAM_VOICE_CALL - plays back on bt headset
        //STREAM_DTMF - sound from loud speaker, not very clear but works for now
        atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, samplingRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
        atrack.setPlaybackRate(samplingRate);
        byte[] buffer = new byte[bufferSize];
        arec.startRecording();
        atrack.play();
        while(isRecording) {
            arec.read(buffer, 0, bufferSize);
            atrack.write(buffer, 0, buffer.length);
        }

蓝牙耳机mic代码配置:

audioManager.setBluetoothScoOn(true);
    audioManager.startBluetoothSco();
    //works for MODE_IN_COMMUNICATION - however there's a lot of echo when on speaker phone
    //works fine for MODE_IN_CALL
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    //turning speakerphone on causes a lot of disturbance
    //audioManager.setSpeakerphoneOn(true);
    //samplingRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_VOICE_CALL);
    samplingRate = 16000;

但是,即使我有setSpeakerphoneOn(false)耳机并将耳机插入音频插孔,音频也是从手机本身而不是耳机播放的。我该如何克服呢?打开扬声器会产生很大的噪音。

我使用的是 16000 的采样率,因为它可以在没有耳机的情况下听到。在我使用原生采样率的那一刻,我显然只是听到了真正的高频噪音。我怎样才能让这个声音在耳机而不是手机的扬声器上播放?

4

1 回答 1

1

对您来说不幸的是,随着显式路由的弃用,不支持蓝牙和免提电话设置的组合,这将使您能够使用蓝牙耳机的麦克风,但使用听筒的扬声器(听筒/耳机或免提电话)。鉴于许多可能的荒谬组合,我怀疑这是转向更简单的 api 的理由。

您可以尝试调用已弃用的 setRouting() 函数,但这可能会产生意想不到的副作用和/或什至不起作用,具体取决于硬件的能力以及在底层子系统/驱动程序中正确管理的情况。

于 2013-09-05T07:49:48.807 回答