Android开发

Android 切换扬声器和听筒

2022-01-19  本文已影响0人  satisfying

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 里添加权限
上一篇下一篇

猜你喜欢

热点阅读