我正在尝试动态启动和停止处理程序线程。为此,我正在使用removecallbacks
。每当我第一次启动removecallbacks
时,它都能正常工作,但是
当我再次尝试执行此操作时,它不起作用,默认情况下,处理程序线程会不断触发。
这是我的代码:
var handler:Handler = Handler()
var mediaPos = mPlayer!!.getCurrentPosition();
var mediaMax = mPlayer!!.getDuration();
seekbr!!.setMax(mediaMax.toFloat()); // Set the Maximum range of the
seekbr!!.setProgress(mediaPos.toFloat());// set current progress to song's
var moveSeekBarThread = object : Runnable {
override fun run() {
if (mPlayer!!.isPlaying()) {
val mediaPos_new = mPlayer!!.getCurrentPosition()
val mediaMax_new = mPlayer!!.getDuration()
seekbr!!.setMax(mediaMax_new.toFloat())
seekbr!!.setProgress(mediaPos_new.toFloat())
handler.postDelayed(this, 100) //Looping the thread after 0.1 second
}
}
}
handler.removeCallbacks(moveSeekBarThread)
handler.postDelayed(moveSeekBarThread, 100)
seekbr!!.setOnSeekChangeListener(object:OnSeekChangeListener {
override fun onSeeking(seekParams: SeekParams) {
}
override fun onStartTrackingTouch(seekBar:IndicatorSeekBar) {
handler.removeCallbacksAndMessages(null)
}
override fun onStopTrackingTouch(seekBar:IndicatorSeekBar) {
if (mPlayer != null && mPlayer!!.isPlaying()) {
mPlayer!!.seekTo(seekBar.getProgress())
handler.postDelayed(moveSeekBarThread,100)
}
}
})
在这里,我试图在onStartTrackingTouch()
和onStopTrackingTouch()
内删除并设置处理程序线程。这段代码在被调用时首次起作用,但在调用后却不起作用。
任何帮助将不胜感激。