Georgiy Chebotarev
Georgiy Chebotarev

Reputation: 3270

Bug in the MODE_IN_COMMUNICATION in android audiomanager?

I think, I found a strange behavior in the MODE_IN_COMMUNICATION. I'm testing it on Android 9.0:

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

After we set the MODE_IN_COMMUNICATION, we lost the rington sounds in the application. For rington I'm using a standard method:

private void warningSound(Context context) {
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You can try set another mode, but after MODE_IN_COMMUNICATION it doesn't help - ringtons still will disabled.

Are you have any ideas of solutions for solve it? (I should switching between headsets and speakers programming, because 3.5 jack always connect to device.)

Upvotes: 1

Views: 1018

Answers (1)

Georgiy Chebotarev
Georgiy Chebotarev

Reputation: 3270

  private void warningSound(Context context) {
    try {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int currentMode = audioManager.getMode();
        audioManager.setMode(AudioManager.MODE_NORMAL);

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();

        new Handler().postDelayed(() -> audioManager.setMode(currentMode), 3000L);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions