3

I'd like to use the speaker for audio output while simultaneously using the headphone jack for a different audio output/input. Is this possible?

user1777060
  • 263
  • 4
  • 8

1 Answers1

4

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

Community
  • 1
  • 1
Michael
  • 57,169
  • 9
  • 80
  • 125
  • This is entirely dependant on the hardware architecture of the phone, and the capabilities of the audio codec. – marko Jan 08 '13 at 10:32
  • @Marko: I know. I pointed that out in the first paragraph. It does work on the U8500. It does *not* work on e.g. the APQ8064. – Michael Jan 08 '13 at 10:51
  • @Michael, is it possible to do that using an AudioTrack? I need AudioTracks to generate sounds on the fly. – the_moon Aug 22 '14 at 17:15
  • @leo2_uru: It doesn't really matter if you use `AudioTrack`, `MediaPlayer` or something else. All those APIs are based on (native) `AudioTrack`s internally anyway (at least last time I checked). – Michael Aug 22 '14 at 18:36
  • How do I set the stream type in an AudioTrack @Michael? I don't think that is possible according to the Android API... – the_moon Aug 22 '14 at 19:14
  • You pass it to the constructor. – Michael Aug 22 '14 at 19:42
  • Hi @Michael this is not related exactly to above question, I'm stuck in call recording app in some of the marshmallow devices. Thing is I'm not able access audio source VOICE_CALL, I tried with audio source MIC but incoming voice not getting recorded. I have seen your many SO answer s related to android audio system. And even I tried with cpp also, not able to figure out how to record voice call. Some other apps are actually works on device which I'm testing.I was working on this problem from past 1 month, Please help me. – Bharath Kumar Aug 09 '17 at 09:11
  • @Michael This is my question link : https://stackoverflow.com/questions/45231232/android-call-recording-incoming-voice-not-getting-recorded – Bharath Kumar Aug 09 '17 at 09:18