It's possible to do on some devices (I've verified that it works on a Sony XPeria P), but I don't think you can count on it to work on all devices.
What you'd do is force MUSIC
streams to route to the loudspeaker:
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);
Then you use the MUSIC
stream for anything you want routed to the loudspeaker, and the VOICE_CALL
stream for anything you want routed to the headset/headphones.
See my answer for Playing sound over speakers while playing music through headphones
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…