[英]Android VideoView detect video is soon finish
I want to know whether the movie is just one minute away from the the end. 我想知道这部电影离结尾是否只有一分钟的路程。 So, I use the below code to detect.
因此,我使用以下代码进行检测。 However, the player is hang and it can't do anything.
但是,播放器挂起,无法执行任何操作。 Could somebody correct my code?
有人可以纠正我的代码吗? Thanks!
谢谢!
public Runnable mPlayToEnd = new Runnable() {
public void run() {
while(true)
{
if(mVideoDisplayView.getDuration() - mVideoDisplayView.getCurrentPosition() <= 1000 * 60)
{
Intent intent = new Intent(WILL_END);
sendBroadcast(intent);
mSendEndEvent = false;
Log.d("123","--Duration = " + mVideoDisplayView.getDuration() + "--");
Log.d("123","--Current = " + mVideoDisplayView.getCurrentPosition() + "--");
Log.d("123","--Soon End--");
}
}
}
};
Handler mCheckEndHandler = new Handler();
mCheckEndHandler.post(mPlayToEnd);
It hangs because you have an infinite while(true) loop, which you never break, and it tries to run full speed to check whether you're 1 minute from the end. 之所以挂起,是因为您有一个无限的while(true)循环,您永远不会中断它,并且它会尝试以最大速度运行,以检查您是否距离结束还有1分钟。 What you could do is to check periodically, eg.
您可以做的是定期检查,例如。 every 1 second, and then process accordingly, so your code might looks something as:
每1秒,然后进行相应处理,因此您的代码可能如下所示:
public Runnable mPlayToEnd = new Runnable() {
@Override
public void run() {
if (mmVideoDisplayView.getDuration() - mVideoDisplayView.getCurrentPosition() <= 1000 * 60) {
Intent intent = new Intent(WILL_END);
sendBroadcast(intent);
mSendEndEvent = false;
Log.d("123","--Duration = " + mVideoDisplayView.getDuration() + "--");
Log.d("123","--Current = " + mVideoDisplayView.getCurrentPosition() + "--");
Log.d("123","--Soon End--");
}
else {
mCheckEndHandler.postDelayed(mPlayToEnd, 1000);
}
}
}
Then declare your handler as class member variable, so that it's accessible in your runnable, and start it: 然后将处理程序声明为类成员变量,以便在您的runnable中可以访问它,然后启动它:
mCheckEndHandler = new Handler();
mCheckEndHandler.post(mPlayToEnd);
What will happen is your runnable will check the condition, and if it's satisfied, it will send the broadcast, otherwise, it will run itself again in 1 second (1000ms). 将会发生的是您的可运行设备将检查条件,如果满足,它将发送广播,否则,它将在1秒(1000毫秒)内再次运行。 The whole process repeats until your condition is true.
重复整个过程,直到满足您的条件为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.