[ACCEPTED]-how to turn speaker on/off programmatically in android 4.0-speaker

Accepted answer
Score: 27

Something like this might work on some devices 10 (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 9 combination FOR_MEDIA, FORCE_SPEAKER is typically only used internally 8 to route the FM-radio audio to the loudspeaker 7 (since the FM-radio requires you to have 6 a wired headset / headphone plugged in to 5 act as an antenna). Devices that don't have 4 FM-radio functionality (or uses an alternative 3 implementation) might ignore this combination 2 of parameters, so this method would not 1 work on such a device.

Score: 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();
                }
            }
        });

0

Score: 5

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

If no accessory 9 connected;

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

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

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

Score: 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 1 Android 7.0(Redmi 4x) and it worked fine.

Score: 2

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

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

Score: 2

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

static AudioManager audioManager =  (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
Score: 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);

0

More Related questions