how to turn speaker on/off programmatically in android 4.0
Asked Answered
G

8

24

I play a file through media player and I want to give options like speaker on/off, play though headset, bluetooth ,etc. I tried the below code which works well for android 2.2 but I want something that can also work for 2.2 and 4.0 both. Can you help me to programmatically turn the speaker on/off and playing via headphones?

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    if(isOn){
        audioManager.setMode(AudioManager.MODE_IN_CALL);    
        audioManager.setMode(AudioManager.MODE_NORMAL); 
    }else{
        //Seems that this back and forth somehow resets the audio channel
        audioManager.setMode(AudioManager.MODE_NORMAL);     
        audioManager.setMode(AudioManager.MODE_IN_CALL);        
    }
    audioManager.setSpeakerphoneOn(isOn);

P.S: I have given this permission in manifest:

android.permission.MODIFY_AUDIO_SETTINGS 
Guib answered 20/8, 2012 at 10:42 Comment(1)
How you solved this problem ?Polly
L
27

Something like this might work on some devices (I've only tested in on an XPeria P):

final static int FOR_MEDIA = 1;
final static int FORCE_NONE = 0;
final static int FORCE_SPEAKER = 1;

Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
setForceUse.invoke(null, FOR_MEDIA, FORCE_SPEAKER);
// To get back to the default behaviour, use the combination FOR_MEDIA,FORCE_NONE.

The combination FOR_MEDIA, FORCE_SPEAKER is typically only used internally to route the FM-radio audio to the loudspeaker (since the FM-radio requires you to have a wired headset / headphone plugged in to act as an antenna). Devices that don't have FM-radio functionality (or uses an alternative implementation) might ignore this combination of parameters, so this method would not work on such a device.

Lithic answered 20/8, 2012 at 12:33 Comment(6)
This works on my Galaxy S2 and android 4.0.4. How did you come up with this idea? This is like a miracleRubin
setForceUse is the method used internally to force certain audio types to a given device. The reason I know that is that I work with Android audio for a living.Lithic
I dislike hacks like this because they are almost certain to be broken by a future release of the Android framework. But this is the only way I have found that actually allows output via the speaker when the headset is plugged in. Works on a Huawei Y300 running Android-4.1.1Redshank
Hey Michael, when we have to set back the default behaviour, that is, let it play through earpeice, do we do setForceUse.invoke(null, 1, 0); ?Sixpenny
Its not working in Android Pie device, any idea why?Arteriole
How can I force a webview to play audio via earpiece?Rochellrochella
B
6
AudioManager mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

Button mVolumeButton = (Button)findViewById(R.id.btn_Volume);
        mVolumeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAudioMgr.isWiredHeadsetOn()){
                    mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                    mAudioMgr.setWiredHeadsetOn(false);
                    mAudioMgr.setSpeakerphoneOn(true);
                    mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);

                    Toast.makeText(getApplicationContext(), "SpeakerPhone On", Toast.LENGTH_LONG).show();
                }else{
                    mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
                    mAudioMgr.setSpeakerphoneOn(false);
                    mAudioMgr.setWiredHeadsetOn(true);
                    Toast.makeText(getApplicationContext(), "Wired Headset On", Toast.LENGTH_LONG).show();
                }
            }
        });
Bushey answered 21/3, 2016 at 12:59 Comment(0)
L
5

You can acquire either a rear speaker or a front earpiece at time.

If no accessory connected;

Use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_NORMAL); & audioManager.setSpeakerphoneOn(true);

If accessory connected; Use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(true);

Note: Make sure audioManager.setWiredHeadsetOn(boolean on) and audioManager.setBluetoothScoOn(boolean on) set to false to route audio via earpiece . And set either to true to route audio accordingly.

Lovable answered 14/3, 2014 at 11:50 Comment(0)
C
4

try follow code snippet:

//for speakerphone on
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);

//for headphone on
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(false);

BTW,I tested in Android 7.0(Redmi 4x) and it worked fine.

Cardialgia answered 24/8, 2018 at 9:4 Comment(2)
This works for me! Yay! Had been struggling with this for a while. My only issue now is that there is about a second of dead air in the transition from speakerphone to receiver. Not a huge deal but a little annoying.Suk
audioManager.setMode(AudioManager.MODE_NORMAL); this line code works for meTachyphylaxis
X
2

Problem solved. For all of you who are still searching for the answer. Well this is not a bug , but just a tricky thing . You need to use the PhoneStateListener

Using this guide : http://danielthat.blogspot.co.il/2013/06/android-make-phone-call-with-speaker-on.html

Xenon answered 22/8, 2014 at 7:52 Comment(0)
S
2

if u just want to open your speakerphone on u just write this line in oncreate() of your activity.

static AudioManager audioManager =  (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
Suspiration answered 29/10, 2014 at 10:12 Comment(0)
U
2

Try This.

AudioManager audioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    if (isOn) {
        isOn = false;
        audioManager.setMode(AudioManager.MODE_IN_CALL);
        audioManager.setMode(AudioManager.MODE_NORMAL);

    } else {
        isOn = true;
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setMode(AudioManager.MODE_IN_CALL);

    }
    audioManager.setSpeakerphoneOn(isOn);
Unrounded answered 20/4, 2018 at 8:51 Comment(0)
P
-1

Try

AudioManager.speakerphone(true);

Look at this.

Puling answered 20/8, 2012 at 10:48 Comment(6)
I guess you mean AudioManager.setSpeakerPhoneOn? (which is what he's already using). There's no speakerphone method in AudioSystem.Lithic
Look at the link i posted . In that link you can find that method.Puling
You wrote AudioSystem in your answer, but linked to the AudioManager reference. That's why I asked.Lithic
@chirag : AudioManager.speakerphone(true); This doesn't work. Could you suggest something else which works both for 2.2 and 4.0Guib
You must also do: audioManager.setMode(AudioManager.MODE_IN_CALL);Rovelli
With call managers and that type of stuff, you have to implement a check to see if it is android 11+ or below that. Two seperate ways of doing things. Keep that in mind. The same goes for when bringing your activity to the front, so code as if it is two different OS's because in some way it is.Corot

© 2022 - 2024 — McMap. All rights reserved.