本文主要是介绍Android 打开/关闭扬声器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
private static int currVolume = 0;
//打开扬声器
public void OpenSpeaker() {
try{
//判断扬声器是否在打开
AudioManager audioManager =(AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);audioManager.setMode(AudioManager.ROUTE_SPEAKER);
//获取当前通话音量
currVolume =audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);if(!audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(true);
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL ),
AudioManager.STREAM_VOICE_CALL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭扬声器
public void CloseSpeaker() {
try {
AudioManageraudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if(audioManager != null) {
if(audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,currVolume,
AudioManager.STREAM_VOICE_CALL);
}
}
} catch (Exception e) {
e.printStackTrace();
}
//Toast.makeText(context,"揚聲器已經關閉",Toast.LENGTH_SHORT).show();
}
这篇关于Android 打开/关闭扬声器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!