本文实例讲述了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程序设计有所帮助。