简体   繁体   English

将数据写入Android音频插孔

[英]Write data to Android audio jack

I'm recording live audio from a bluetooth headset's mic and playing it onto the phone's speaker. 我正在录制蓝牙耳机麦克风的现场音频并将其播放到手机的扬声器上。

Streaming code: 流媒体代码:

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

Configuration of bluetooth headset mic code: 配置蓝牙耳机麦克风码:

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;

However, even when I have setSpeakerphoneOn(false) and insert a earphones into the audio jack, the audio is played from the phone itself and not the headset. 但是,即使我有setSpeakerphoneOn(false)并将耳机插入音频插孔,音频也会从手机本身而不是耳机播放。 How do I overcome this? 我该如何克服这个问题? Turning the loudspeaker on causes a lot of noise. 打开扬声器会产生很多噪音。

I'm using a sampling rate of 16000 because it can be heard without headphones. 我使用16000的采样率,因为它可以在没有耳机的情况下听到。 The moment I use the native sampling rate, I obviously just hear really high frequency noise. 在我使用原生采样率的那一刻,我显然只听到了高频噪声。 How can I get this sound to play on the headset rather than the phone's speaker? 如何在耳机而不是手机的扬声器上播放此声音?

Unfortunately for you, with the deprecation of explicit routing there is no supported combination of bluetooth and speakerphone settings which will enable you to use the bluetooth headset's microphone but the handset's speakers (earpiece/headset or speakerphone). 不幸的是,随着显式路由的弃用,没有支持蓝牙和扬声器设置的组合,这将使您能够使用蓝牙耳机的麦克风,但手机的扬声器(耳机/耳机或扬声器电话)。 Given the many possible nonsensical combinations, I suspect that's the rationale for moving to the simpler api. 鉴于许多可能的荒谬组合,我怀疑这是转向更简单的api的理由。

You could try calling the deprecated setRouting() function, however this may have unexpected side effects and/or not even work depending upon what the hardware is capable of and what cases are correctly managed in the underlying subsystems/drivers. 您可以尝试调用已弃用的setRouting()函数,但这可能会产生意外的副作用和/或甚至无法工作,具体取决于硬件的功能以及在底层子系统/驱动程序中正确管理的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM