So I am trying to use some code found here on Stack Overflow in order to route audio from the Headset to the Speakers.
What I am currently doing is:
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.STREAM_MUSIC);
am.setSpeakerphoneOn(true);
sleep(15000);
What happens is that the audio is routed during the "sleep", however it gets back to the headset after the 15 seconds. If I perform this without the sleep, the sound will go to the speaker and back to the headset very quickly.
After this call, my program dies. I would like that this would persist in the system, as the app Headset Toggle does
I could not discover why my method wasn't working, but I found a workaround for it.
What I am doing at the moment is:
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
// First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go back to the default
// behavior, use FORCE_NONE (0).
setForceUse.invoke(null, 1, 1);
Thanks to Michael from this post.