6

I have an AudioTrack streaming via headphones. I have to send a SoundPool to built-in speakers only, without interrupting the AudioTrack playing over the headphones. Any hacks gangsters?

Ravi Tej Vupasi
  • 73
  • 1
  • 2
  • 4
  • so the SoundPool is separate from the AudioTrack right? – Soham Aug 18 '12 at 09:06
  • AudioManager mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); mAudioMgr.setSpeakerphoneOn(true); mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION); – Jatinkumar Patel Mar 21 '16 at 12:55

1 Answers1

12

Many Android devices use a single output thread in the AudioFlinger / audio HAL for all local playback (earpiece, speaker, wired headset/headphones), making different routing of two tracks simultaneously impossible (which is why on many devices the media streams are forcibly muted if a notification is played and you've got a wired headset attached; because otherwise you'd hear the music in the loudspeaker while the notification is played).

On some devices it might be possible to do what you're looking for if you manage to do a setForceUse(FOR_MEDIA, FORCE_SPEAKER) and use the MUSIC stream type for the stuff you want to play in the loudspeaker, and the VOICE_CALL stream type for the stuff that you want to play in the wired headset.
I'm not sure if there's any way for an application to perform that setForceUse call though. Perhaps you can get at the handleMessage method of the AudioService class through reflection and send it an MSG_SET_FORCE_USE message.. I've never tried it myself so it might fail miserably.

EDIT: I've now tested the setForceUse way of forcing MEDIA streams to the loudspeaker while a wired headset is attached on an actual device, and it does work (though I can't guarantee that it will work across all devices). The implementation was slightly different from what I described above. See my answer to how to turn speaker on/off programatically in android 4.0 for the code I used.

Community
  • 1
  • 1
Michael
  • 57,169
  • 9
  • 80
  • 125
  • using STREAM_RING helps to redirect the sound to speaker even if the headphones are connected. However, the sound playing on Headphones (AudioTrack) is paused until this STREAM_RING is completed and then resumes. My priority is playing the headphones stream without interruption. – Ravi Tej Vupasi Aug 20 '12 at 13:32
  • Yes, STREAM_RING follows the SONIFICATION routing strategy, which would trigger the behavior I described in the first paragraph of my answer. I've verified that a MUSIC stream forced to speaker + a VOICE_CALL stream in wired headset works properly on an XPeria P, but I haven't tested it on any other devices. – Michael Aug 20 '12 at 14:06
  • Mike, I am sorry to push you too hard but, I have been testing it on S2 and HTC One X. They both are pausing the audio on headphones. Mine is a signal which up on breaking continuity will breach the working process of the system. – Ravi Tej Vupasi Aug 22 '12 at 11:37
  • Which stream types did you use? – Michael Aug 22 '12 at 11:48
  • for headphones STREAM_MUSIC, for speaker STREAM_RING. We have a peripheral connected which converts sound to IR signal. and some special effects like explosions and grenades will be played through speakers. – Ravi Tej Vupasi Aug 22 '12 at 13:17
  • 2
    That combination won't work for the reason I explained earlier. The only combination that I was able to get this to work for was MUSIC + VOICE_CALL. – Michael Aug 22 '12 at 14:08