Is there any way to force audio through the speakers when headphones are plugged in?

后端 未结 5 1902
悲&欢浪女
悲&欢浪女 2020-12-02 21:02

I\'ve tried the recommended

setSpeakerphoneOn(true) 

and the unrecommended

AudioSystem.setForceUse(AudioSystem.FOR_MEDIA,          


        
相关标签:
5条回答
  • 2020-12-02 21:08

    Thanks to some comments here, and after some search I finally found out how to do this.

    My code below is called when I press a button. It sets up audio for activating speakers while calling, then calls the number.

    Pay attention to the comments in my code regarding permissions.

    buttonRep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //1) we need to detect once the call has been established
            //Note : this requires READ_PHONE_STATE permission in your manifest
    
            final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    
            tm.listen(new PhoneStateListener()
            {
                @SuppressWarnings("unused")
                @Override
                public void onCallStateChanged (int state, String incomingNumber)
                {
                    if(state == TelephonyManager.CALL_STATE_OFFHOOK) //Here we are
                    {
                        //2) we switch sound to speakers
                        //Note : this requires MODIFY_AUDIO_SETTINGS permission in your manifest
                        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                        am.setMode(AudioManager.MODE_IN_CALL);
                        am.setSpeakerphoneOn(true);
                        int i = state;
    
                        //3) we don't need this call listener anymore so we "destroy it"
                        tm.listen(this, PhoneStateListener.LISTEN_NONE);
                    }
                }
            }
    
            , PhoneStateListener.LISTEN_CALL_STATE);
    
            //4) we are ready to place our call  now
            //Note : this requires CALL_PHONE permission in your manifest
            Intent action = new Intent();
            action.setAction(Intent.ACTION_CALL);
            action.setData(Uri.parse("tel:660")); //660 is my answering-machine number :)
            MainActivity.this.startActivity(action);
        }
    });
    
    0 讨论(0)
  • 2020-12-02 21:09

    Try this:

        AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
        IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        Intent iStatus = getApplicationContext().registerReceiver(null, iFilter);
        boolean isHeadsetOn = false;
    
        if (iStatus != null) {
            isHeadsetOn = iStatus.getIntExtra("state", 0) == 1;
        }
        if(mAudioManager.isWiredHeadsetOn() || isHeadsetOn)
        {
            //When headphones are plugged
            mAudioManager.setMode(AudioManager.MODE_CURRENT);
            mAudioManager.setSpeakerphoneOn(true);
        }else { 
            //When headphones are not plugged
            mAudioManager.setMode(AudioManager.MODE_IN_CALL);
            mAudioManager.setSpeakerphoneOn(true);
        }
    
    0 讨论(0)
  • 2020-12-02 21:16

    Try doing this:

    AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    if(manager.isWiredHeadsetOn())
    {
        manager.setWiredHeadsetOn(false);
        manager.setSpeakerphoneOn(true); 
        manager.setRouting(AudioManager.MODE_CURRENT, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);  
        manager.setMode(AudioManager.MODE_CURRENT); 
    }
    

    You will need this permission:

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    
    0 讨论(0)
  • 2020-12-02 21:23

    This worked for me

    First, I added permissions in Manifest.xml

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

    Then, I used this:

    AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
    m_amAudioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
    m_amAudioManager.setSpeakerphoneOn(true);
    
    0 讨论(0)
  • 2020-12-02 21:33

    The answer turned out to be the following, with tips from Android - Getting audio to play through earpiece

    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    

    Ignore setRouting, it does nothing in APK > 10. Ignore comments about setMode. Ignore comments about setWiredHeadsetOn. Make sure you have MODIFY_AUDIO permissions.

    0 讨论(0)
提交回复
热议问题