1.添加权限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
2.获取AudioManager
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
3.切换
private void setSpeakerphoneOn(boolean on) {
if(on) {
audioManager.setSpeakerphoneOn(true);
} else {
audioManager.setSpeakerphoneOn(false);//关闭扬声器
audioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
//把声音设定成Earpiece(听筒)出来,设定为正在通话中
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
}
tip:一定要注意 manifest 里添加权限
网友评论