Android的搜索栏的正确使用? [英] Correct usage of Android's SeekBar?
问题描述
我试图堵漏搜索栏
进入我的视频应用程序,但它似乎并没有以往任何时候都达到栏的末尾。这里的code:
I tried plugging a SeekBar
into my video app, but it doesn't seem to ever reach the end of the bar. Here's the code:
videoSeekBar = (SeekBar) activity.findViewById(R.id.videoPlayerSeek);
videoDuration = player.getDuration();
videoSeekBar.setMax(videoDuration);
videoSeekBar.setProgress(0);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (videoPlayProgress < videoDuration) {
videoPlayProgress = player.getCurrentPosition();
// Update the progress bar
handler.post(new Runnable() {
public void run() {
videoSeekBar.setProgress(videoPlayProgress);
}
});
}
}
}).start();
然后,我有一个 OnCompleteListener
连接到的MediaPlayer。当它被调用时,
Then, I have an OnCompleteListener
attached to the MediaPlayer. When it is called,
player.getCurrentPosition()
返回6209
player.getDuration()
返回6592
这不可能是正确的。此时,滑块状态指示器是不太在滑块的末端,但有一个微小的间隙。该视频的长度较短,更大的差距。
Which can't be right. At this point, the slider status indicator isn't quite at the end of the slider bar, but there's a slight gap. The shorter the video's length, the bigger the gap.
推荐答案
一,审议&LT; =
,而不是&LT;
。
更重要的是,你不需要或不想要这个忙循环后台线程。喜欢的东西去:
More importantly, you do not need or want a busy-loop background thread for this. Go with something like:
Runnable r=new Runnable() {
public void run() {
videoSeekBar.setProgress(player.getCurrentPosition());
if (player.getCurrentPosition()<player.getDuration()) {
videoSeekBar.postDelayed(r, 33);
}
}
};
和地方叫运行()
在研究
,当你的视频启动。这将每秒更新搜索栏
30倍左右。
and somewhere call run()
on r
, when your video starts up. This will update the SeekBar
about 30 times per second.
您目前的实现是前台繁忙的循环,张贴数千封邮件到主应用程序线程的队列中,这将使您的视频播放性能较差,废旧电池对没有特别的优势。
Your current implementation is a foreground busy loop, posting thousands of messages onto the main application thread's queue, which will make your video playback performance worse and waste battery for no particular advantage.
这仍然可能无法让所有的方式来结束 - 这有可能是视频将达到 setProgress()
呼叫和<$ C $之间到底C>如果测试。它是在最后的真正考验是 OnCompletionListener
,所以只要定位搜索栏
末在该点
This still might not get all the way to the end -- it's possible that the video will reach the end between the setProgress()
call and the if
test. The true test of it being at the end is the OnCompletionListener
, so just position the SeekBar
at the end at that point.
这篇关于Android的搜索栏的正确使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!