hiwehuvelu
hiwehuvelu

Reputation: 13

Play sound in speaker while headphone is connected

How to play sound in speaker while a headphone or Bluetooth headset is connected?

I have tried the code below, but still the same:

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(true);

Upvotes: 1

Views: 4030

Answers (1)

Jyubin Patel
Jyubin Patel

Reputation: 1373

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 for the code I used below link.

how to turn speaker on/off programmatically in android 4.0

hope it helps you.

Upvotes: 2

Related Questions