android控制系统音量

body_sb=(SeekBar)root.findViewById(R.id.body_sb);
audioManager=(AudioManager)getActivity().getSystemService(Service.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //获取系统最大音量
body_sb.setMax(maxVolume); //拖动条最高值与系统最大声匹配
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
body_sb.setProgress(currentVolume);
body_sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
body_sb.setProgress(currentVolume);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
posted @ 2016-09-05 16:44  麦子TMAC  阅读(247)  评论(0编辑  收藏  举报