将数据写入Android音频插孔

时间:2013-08-27 16:44:12

标签: android audio bluetooth buffer microphone

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

流媒体代码:

 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);
        }

配置蓝牙耳机麦克风码:

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的采样率,因为它可以在没有耳机的情况下听到。在我使用原生采样率的那一刻,我显然只听到了高频噪声。如何在耳机而不是手机的扬声器上播放此声音?

1 个答案:

答案 0 :(得分:1)

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

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