通过接收广播进行改变
private AudioManager mAudioManager;
private ContentObserver mVoiceObserver;
private MyVolumeReceiver mVolumeReceiver;
sbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
//系统音量和媒体音量同时更新
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, i, 0);
audioManager.setStreamVolume(3, i, 0);// 3 代表 AudioManager.STREAM_MUSIC
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
设置音量条的进度。
mVoiceObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
sbVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
}
};
private void initVolume() {
mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);//获取媒体系统服务
sbVolume.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));//设置最大音量
sbVolume.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));//当前的媒体音量
myRegisterReceiver();//注册同步更新的广播
}
private void myRegisterReceiver(){
mVolumeReceiver = new MyVolumeReceiver() ;
IntentFilter filter = new IntentFilter() ;
filter.addAction("android.media.VOLUME_CHANGED_ACTION") ;
getContext().registerReceiver(mVolumeReceiver, filter) ;
}
private class MyVolumeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//如果音量发生变化则更改seekbar的位置
if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) ;// 当前的媒体音量
sbVolume.setProgress(currVolume) ;
}
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
getContext().unregisterReceiver(mVolumeReceiver);
ButterKnife.unbind(this);
}
每一秒钟改变一次。
private boolean isSeekBarChanging;//互斥变量,防止进度条与定时器冲突。
private Timer timer ;
private TimerTask timerTask;
sbProgress.setMax(MusicUtil.getInstance().getDuration());
timerTask = new TimerTask() {
@Override
public void run() {
if(isSeekBarChanging==true) {
return;
}
sbProgress.setProgress(MusicUtil.getInstance().getCurrentPosition());
//更改UI界面需要切换到主线程中去 所以要在这里重写一个这个方法
runOnUiThread(new Runnable() {
@Override
public void run() {
tvCurrentTime.setText(formatTime("mm:ss",MusicUtil.getInstance().getCurrentPosition()));
}
});
}
};
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
sbProgress.setOnSeekBarChangeListener( new MySeekbar());//添加一个改变监听
class MySeekbar implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
isSeekBarChanging=true;//防止滑动进度条时候起冲突
}
public void onStopTrackingTouch(SeekBar seekBar) {
MusicUtil.getInstance().setSeekTo(seekBar.getProgress()); //把拖动后进度条的进度传入然后更改歌曲播放时间
isSeekBarChanging=false;
}
}
public static String formatTime(String pattern, long milli) {
int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
String mm = String.format(Locale.getDefault(), "%02d", m);
String ss = String.format(Locale.getDefault(), "%02d", s);
return pattern.replace("mm", mm).replace("ss", ss);
}