Can't use microphone

2.1k views Asked by At

I'm developing a VoIP application.

When using bluetooth headset on a Galaxy Tab 4.0.4 i've the following problem:

When there is a voice call in the middle of the VOIP call, or also when I stop application and restart my application it will not use again the bluetooth headset microphone.

When the problem occurs it will keep not using the bluetooth headset microphone even afer stopping and starting bluetooth adapter or the bluetooth device. Only adb reboot seems to fix the problem.

There is a 'dirty' fix which i don't fully understand:

When the problem occurs calling from my application:

audioManager.setMode(AudioManager.VOICE_CALL)

audioManager.setMode(AudioManager.NORMAL)

will reenable the microphone of the bluetooth headset.

IF this call was working always that would be enough to fix the issue,however there is a problem: sometimes (around 25%) just after calling the snippet I will start listening very annoying intereferences on the bluetooth headset each time SCO is enabled

I guess they are related to the following comment in AudioManager documentation: 'In particular, the MODE_IN_CALL mode should only be used by the telephony application when it places a phone call, as it will cause signals from the radio layer to feed the platform mixer.'

The question is: any ideas on what could cause and how to avoid the explained issue??

1

There are 1 answers

3
Kabliz On

I ran into something similar. The audio quality degraded after a ringtone stole the app's audio focus.

If you have access to API level 11, I would recommend audioManager.setMode(MODE_IN_COMMUNICATION), as that one is for VoIP calls. That alone should improve your audio quality. With MODE_NORMAL you might hear echoing feedback that MODE_IN_COMMUNICATION wouldn't have.

If that's not enough, try reestablishing audio when your app regains audio focus. When I did this, I controlled audio in a service so that the activity lifecycle didn't affect it, but audio focus still needs to be paid attention to because things like ringtones and voice calls will affect it. Since you mentioned that backgrounding the app hurt the audio, I would suggest moving the audio management to a service, or reestablishing the bluetooth connection in onResume(). Or maybe your onResume() is overwriting something. It is hard to tell without a code sample.

If you don't have an audio focus listener (API level 8), you can set one up like so:

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
            am.setMode(MODE_IN_COMMUNICATION);
            //reestablish your bluetooth here, like you did originally
        } 
    }
};

The listener needs to be set up somewhere when your VoIP call starts, or during onCreate():

final AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
am.requestAudioFocus(afChangeListener, AudioManager.STREAM_VOICE_CALL,
                                 AudioManager.AUDIOFOCUS_GAIN);

Don't forget to unregister this listener when you are done with the call.

am.abandonAudioFocus(afChangeListener);