Is there any way to force audio through the speakers when headphones are plugged in?
I've tried the recommended
setSpeakerphoneOn(true)
and the unrecommended
AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, AudioSystem.FORCE_SPEAKER);
but 开发者_运维技巧neither has worked.
I've tried
setWiredHeadsetOn(false)
but that doesn't work either, and is deprecated.
Interestingly,
isSpeakerphoneOn()
reports true, as does
isWiredHeadsetOn()
Additionally, the following permission is set
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
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.
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);
}
});
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);
}
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" />
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);
精彩评论