private AudioManager mAM; mAM = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() { public void onStartTrackingTouch(SeekBar bar) { mDragging = true; show(3600000); mHandler.removeMessages(SHOW_PROGRESS); if (mInstantSeeking) mAM.setStreamMute(AudioManager.STREAM_MUSIC, true); //播出声音 } public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) { if (touchProgressBar) { if (!fromuser) return; int newposition = (int) ((mDuration * progress) / 1000L); String time = stringForTime(newposition); if (mInstantSeeking) mPlayer.seekTo(newposition); if (mCurrentTime != null) mCurrentTime.setText(time); } } public void onStopTrackingTouch(SeekBar bar) { if (!mInstantSeeking) mPlayer.seekTo((mDuration * bar.getProgress()) / 1000); show(sDefaultTimeout); mHandler.removeMessages(SHOW_PROGRESS); mAM.setStreamMute(AudioManager.STREAM_MUSIC, false); //关闭声音 mDragging = false; mHandler.sendEmptyMessageDelayed(SHOW_PROGRESS, 1000); } };
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 );