Android 打开/关闭扬声器的代码
jopen
11年前
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(); }