Android关闭和打开Speaker(扬声器) Android开发之关闭和打开Speaker(扬声器)的方法
nosxcy 人气:0想了解Android开发之关闭和打开Speaker(扬声器)的方法的相关内容吗,nosxcy在本文为您仔细讲解Android关闭和打开Speaker(扬声器)的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,关闭,打开,Speaker,扬声器,下面大家一起来学习吧。
本文实例讲述了Android开发之关闭和打开Speaker(扬声器)的方法。分享给大家供大家参考,具体如下:
private int currVolume = 0; /** * 打开扬声器 */ private void openSpeaker() { try{ AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager.setMode(AudioManager.ROUTE_SPEAKER); currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL); if(!audioManager.isSpeakerphoneOn()) { //setSpeakerphoneOn() only work when audio mode set to MODE_IN_CALL. audioManager.setMode(AudioManager.MODE_IN_CALL); 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 { AudioManager audioManager = (AudioManager) 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(); } }
希望本文所述对大家Android程序设计有所帮助。
加载全部内容