AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//1.通话音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d(”VIOCE_CALL”, “max : ” + max + ” current : ” + current);
//2.系统音量
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);
getStreamVolume 是获取当前声音的进度
getStreamMaxVolume 是获取最大的音量值
设置音量的方法也很简单,AudioManager提供了方法:
public void setStreamVolume(int streamType, int index, int flags)
这样设置就行:mAudioManager.setStreamVolume(AudioManager.STREAM_RING,current1,0);
播放系统声音
private void openMediaPlayerVoice() {
Uri mediaUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mMediaPlayer = MediaPlayer.create(this, mediaUri);
if (mMediaPlayer != null) {
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
}
private void closeMediaPlayerVoice() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
开启/关闭震动
/**
* 开启震动
*/
private
void
startVibrate() {
if
(vibrator ==
null
) {
//获取震动服务
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
//震动模式隔1秒震动1.4秒
long
[] pattern = {
1000
,
1400
};
//震动重复,从数组的0开始(-1表示不重复)
vibrator.vibrate(pattern,
0
);
}
/**
* 停止震动
*/
private
void
stopViberate() {
if
(vibrator !=
null
) {
vibrator.cancel();
}
}
静音:
设置系统声音为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"/>