Android 获取、设置铃声、音量、静音、扬声器

通过api获取、设置铃声、音量、静音、扬声器其实挺简单的。
[java] view plain
copy


//首先获取一个AudioManager  
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);   
  
//通话音量   
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );   
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );   
Log.d(”VIOCE_CALL”, “max : ” + max + ” current : ” + current);   
  
//系统音量   
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );   
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );   
Log.d(”SYSTEM”, “max : ” + max + ” current : ” + current);   
  
//铃声音量   
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );   
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );   
Log.d(”RING”, “max : ” + max + ” current : ” + current);   
  
//音乐音量   
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );   
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );   
Log.d(”MUSIC”, “max : ” + max + ” current : ” + current);   
  
//提示声音音量   
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );   
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );   

Log.d(”ALARM”, “max : ” + max + ” current : ” + current);   


 设置音量的方法也很简单,AudioManager提供了方法: 

[java] view plain

public void setStreamVolume(int streamType, int index, int flags)   

streamType为铃声类型,例如:AudioManager.STREAM_VOICE_CALL、AudioManager.STREAM_SYSTEM等,index为音量大小
falgs为标志位。

设置振动:

</pre><pre name="code" class="java"><span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;"><span style="font-family:monospace;"><span style="white-space: pre;"></span></span></span></span><pre name="code" class="java">mVibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);     
                long[] pattern = {150, 100}; // OFF/ON/OFF/ON...    
                mVibrator.vibrate(pattern, -1);  

静音:
设置系统声音为0就行


//通话时设置静音  
System.out.println("isMicrophoneMute =" + audioManager.isMicrophoneMute());  
    audioManager.setMicrophoneMute(!audioManager.isMicrophoneMute());  
   
//通话时设置免提  
System.out.println("isSpeakerphoneOn =" + audioManager.isSpeakerphoneOn());  
    audioManager.setSpeakerphoneOn(!audioManager.isSpeakerphoneOn());  


//别忘了添加权限 

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

摘自:http://blog.csdn.net/qianhen136/article/details/7409775






你可能感兴趣的:(Android 获取、设置铃声、音量、静音、扬声器)