how to turn speaker on/off programmatically in android 4.0

不问归期 提交于 2019-11-26 17:34:10

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.

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

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.

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

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);

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);

try follow code snippet:

//for spearphone 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.

Try

AudioManager.speakerphone(true);

Look at this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!