how to turn speaker on/off programmatically in android 4.0

元气小坏坏 提交于 2019-11-26 05:28:42

问题


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 

回答1:


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.




回答2:


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



回答3:


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.




回答4:


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




回答5:


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



回答6:


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



回答7:


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.




回答8:


Try

AudioManager.speakerphone(true);

Look at this.



来源:https://stackoverflow.com/questions/12036221/how-to-turn-speaker-on-off-programmatically-in-android-4-0

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