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?