4

Please refer my question here. This question is an extension to that:

How to check and redirect audio between wired headset and speaker phone?

I was able to partially solve my problem by including the permissions

  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

in my Android Manifest for that same piece of code. Now, I am able to enable and disable the phone speakers, but I am not able to turn off my wired headset connection programmatically, when the wired headset is still physically connected to the phone.

Can someone please help me here? Is there a specific intent I can use to disable and enable the wired headset connection?

Community
  • 1
  • 1
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • It is assumed that if you plugged in a headset you did so because you wanted to use it. Therefore the headset has higher routing priority than the built-in audio devices (except in a few special cases, like when you enable speakerphone mode during a voice call). An app can't "unplug" a headset programatically. – Michael Jan 07 '14 at 07:39
  • Ok but can I disable the headset connection itself (programmatically)? – SoulRayder Jan 07 '14 at 08:32
  • Or can I *selectively* mute the audio routed to headset? See my more recent question: http://stackoverflow.com/questions/20965530/how-to-mute-audio-in-headset-but-let-it-play-on-speaker-programmatically – SoulRayder Jan 07 '14 at 08:37
  • _"Ok but can I disable the headset connection itself (programmatically)?"_. Like I said; "An app can't "unplug" a headset programatically". In other words, "No". – Michael Jan 07 '14 at 08:39
  • 1
    But there are certain existing apps in the market which perform this *functionality*, for example, *SoundAbout* android app. https://play.google.com/store/apps/details?id=com.woodslink.android.wiredheadphoneroutingfix . Any idea how they might be doing this? – SoulRayder Jan 07 '14 at 08:43
  • Like you've already discovered yourself, there are certain situations where you can force the sound to one device or another. For example `setSpeakerphoneOn` during a call, or [this way](http://stackoverflow.com/a/12037719/1524450) of forcing the media audio to the loudspeaker (might not work on all phones / Android versions). But there's no single method for making the phone behave as if the headset has been unplugged; different use-cases require different methods. And I doubt that the app you linked to actually works on all phones for all use-cases. – Michael Jan 07 '14 at 08:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44689/discussion-between-gautham-and-michael) – SoulRayder Jan 07 '14 at 09:20
  • Alternately, can I **mute** or *reduce the volume* of the audio passing into the headset *alone*? This is a work around, so is it possible? See my question here: http://stackoverflow.com/questions/20965530/how-to-mute-audio-in-headset-but-let-it-play-on-speaker-programmatically and please let me know if this can be done. – SoulRayder Jan 07 '14 at 09:29
  • @Michael: I got an answer for a *work-around* to this problem. Check my link for FYI. http://stackoverflow.com/questions/20965530/how-to-mute-audio-in-headset-but-let-it-play-on-speaker-programmatically – SoulRayder Jan 08 '14 at 05:59
  • Old thread but may be a solution : http://stackoverflow.com/a/30049718/4850790 – Mattt1438 May 05 '15 at 10:02

1 Answers1

0

I looked for a solution online and found something

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                case 0:
                    audioManager.setSpeakerphoneOn(true);
                    break;
                case 1:
                    audioManager.setSpeakerphoneOn(false);
                    break;
                default:
                    audioManager.setSpeakerphoneOn(true);
            }
        }
    }
}

Hope it helps

If you want to visit the site , here is the link

Kevin Kurien
  • 812
  • 6
  • 14