[英]SeekBar - MediaPlayer lag when updating progress while playing
This question has been asked a few times, but I still could not find a answer. 这个问题已经问过几次了,但我仍然找不到答案。
I'm updating the progress on my SeekBar
using a Handler
, like this: 我正在使用
Handler
更新SeekBar
上的进度,如下所示:
Handler handler = new Handler();
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
mSeekBar.setProgress(mPlayer.getDuration());
handler.postDelayed(runnableCode, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);
After looking at this question. 看完这个问题。
The problem is that I'm experiencing lag. 问题是我遇到了滞后。 Every time the
SeekBar
gets update, there is a small lag. 每次
SeekBar
更新时,都会有一个小滞后。
Please note that I'm not talking about when the user 'manually' selects the SeekBar
. 请注意,我不是在说用户何时“手动”选择
SeekBar
。 I know I should check if(fromUser)
in onProgressChanged
when doing that. 我知道我应该在这样做时检查
onProgressChanged
if(fromUser)
。
Also, I did try a Timer
with the same result. 另外,我确实尝试了一个具有相同结果的
Timer
。
Has anybody experienced this issue and how should I going about resolving this? 有没有人遇到过这个问题,我应该如何解决呢?
Use an Asynctask in the Background to move your SeekBar: 在后台使用Asynctask来移动您的SeekBar:
Asynctask : https://developer.android.com/reference/android/os/AsyncTask Asynctask: https : //developer.android.com/reference/android/os/AsyncTask
@Override
protected Integer doInBackground(Void... params) {
if(MainActivity.playing) {
int percent;
int position = MainActivity.Player.getCurrentPosition();
int timeRunning = MainActivity.Player.getDuration();
if (timeRunning == 0 || position == 0) {
percent = 0;
} else {
percent = (position * 100) / timeRunning;
}
return percent;
}
return -1;
}
@Override
protected void onPostExecute(Integer result) {
if(result != -1){
MainActivity.Mbar.setProgress(result);}
}
and then execute this task once in a secound with a timer in your Activity: 然后在“活动”中用计时器执行一次此任务:
Timer: https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html 计时器: https : //docs.oracle.com/javase/7/docs/api/java/util/Timer.html
time.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new AsyncTask().execute();
}
},0,1000);
Hope that helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.