我尝试了推荐的
setSpeakerphoneOn(true)
和未经推荐的
AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, AudioSystem.FORCE_SPEAKER);
但两项都没有奏效。
我试过
setWiredHeadsetOn(false)
但这也不起作用,并且已弃用。
有趣的是,
isSpeakerphoneOn()
报告为真,
isWiredHeadsetOn()
此外,还设置了以下权限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
答案 0 :(得分:28)
答案结果如下,提示来自 Android - Getting audio to play through earpiece
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
忽略setRouting,它在APK&gt;中没有任何作用10.忽略有关setMode的注释。忽略有关setWiredHeadsetOn的注释。确保您拥有MODIFY_AUDIO权限。
答案 1 :(得分:2)
感谢此处的一些评论,经过一番搜索,我终于找到了如何做到这一点。
按下按钮时会调用下面的代码。它设置用于在呼叫时激活扬声器的音频,然后拨打该号码。
请注意我的代码中有关权限的评论。
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);
}
});
答案 2 :(得分:2)
试试这个:
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);
}
答案 3 :(得分:1)
尝试这样做:
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);
}
您需要此权限:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
答案 4 :(得分:0)
这对我有用
首先,我在Manifest.xml中添加了权限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
然后,我用了这个:
AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(true);