AudioManager audiomanage = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audiomanager就是我们定义的控制系统声音的对象。
//音量控制,初始化定义
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//最大音量
int
maxVolume =mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//当前音量
int
currentVolume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
直接控制音量的多少:
1
2
3
4
5
|
if
(isSilent){
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
0
,
0
);
}
else
{
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,tempVolume,
0
);
//tempVolume:音量绝对值
}
|
以一步步长控制音量的增减,并弹出系统默认音量控制条:
1
2
3
4
5
6
7
8
9
10
|
//降低音量,调出系统音量控制
if
(flag ==
0
){
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,
AudioManager.FX_FOCUS_NAVIGATION_UP);
}
//增加音量,调出系统音量控制
else
if
(flag ==
1
){
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,
AudioManager.FX_FOCUS_NAVIGATION_UP);
}
|
//通话音量
int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL );
//系统音量
int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM );
//铃声音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING);
current = mAudioManager.getStreamVolume(AudioManager.STREAM_RING );
//音乐音量
max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC );
//提示声音音量
max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM );