2

I have an app which plays audio through the speaker while a headset is connected. To achieve this I do:

AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(true);

It worked well until Android 5. Does anybody know how to make it work with lollipop?

Mike Akers
  • 12,039
  • 14
  • 58
  • 71
AndreySko
  • 21
  • 2
  • Have you tried using MODE_NORMAL ? Because documentation says " 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." see http://developer.android.com/reference/android/media/AudioManager.html#setMode%28int%29 – Amit Apr 16 '15 at 10:04
  • 1
    Yes, I tried. But audio goes to headset for MODE_NORMAL as well as for MODE_IN_CALL. – AndreySko Apr 16 '15 at 11:21
  • Are you playing a `MUSIC` stream? If so, try using `setForceUse` instead [as described here](http://stackoverflow.com/a/12037719/1524450). – Michael Apr 16 '15 at 13:24
  • Yes, I use MUSIC stream, and I tried that (setForceUse) as well, unfortunately it doesn't work. Audio is still routed to headset. – AndreySko Apr 16 '15 at 13:37

1 Answers1

0

You cannot use AudioManager.MODE_IN_CALL in your application in android lollipop. Use code as :

AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(true);

This will route you audio normally to speaker even though headset is connected. Github repo for more info: https://github.com/sauravpradhan/Basic-Audio-Routing

Saurav
  • 560
  • 4
  • 17