音频播放器的开发.如何在用户操作时停止更新seekbar通知(媒体样式)



我正在开发一个音频播放器,创建一个媒体风格的通知,带有播放、暂停和搜索栏按钮。搜索栏每半秒更新一次,取媒体播放器当前位置的值。一切都很好,直到用户独立地开始倒回搜索栏。

当用户移动滑块时,会触发回调:

MediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onSeekTo(long pos) {
mediaPlayer.seekTo((int)position);
}

但由于搜索栏每半秒更新一次,用户无法无限期地移动搜索栏滑块。问题:当用户操作搜索栏时,我如何才能停止更新它。我知道seekbar有回调:

private boolean isTouch;
public void setSeekbar(SeekBar seekBarPlayer) {
seekBarPlayer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouch = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouch = false;
}
});
}

但我无法拦截它们,因为我没有引用当前的搜索栏来覆盖回调。

尝试使用

mediaSession.setPlaybackState(
PlaybackStateCompat.Builder()
.setState(
PlaybackStateCompat.STATE_PLAYING,

// Playback position.
// Used to update the elapsed time and the progress bar. 
mediaPlayer.currentPosition.toLong(), 

// Playback speed. 
// Determines the rate at which the elapsed time changes. 
playbackSpeed
)
// isSeekable. 
// Adding the SEEK_TO action indicates that seeking is supported 
// and makes the seekbar position marker draggable. If this is not 
// supplied seek will be disabled but progress will still be shown.
.setActions(PlaybackStateCompat.ACTION_SEEK_TO)
.build()
)

最新更新