检测 VideoVIew 是否正在缓冲
有谁知道是否可以检测 VideoView 何时缓冲?
我想在视频缓冲时显示 ProgressDialog。
到目前为止,我尝试使用 OnPreparedListener,但这仅在视频首次加载时有效。如果视频正在播放并且用户将滑动条移动到不同的点,则视频即使正在缓冲,仍然处于“准备就绪”状态。
我还尝试了(我知道这很糟糕)一个只忙于等待 isPlaying() 的 AsyncThread:
private class BufferTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...voids) {
final VideoView videoView = (VideoView)findViewById(R.id.video);
while (!videoView.isPlaying()) { }
return null;
}
protected void onPostExecute(Void v) {
// Hide the dialog here...
}
}
这不起作用,因为一旦您调用 start(),VideoView 似乎就会被视为正在播放,即使它正在缓冲。
我能想到的唯一解决方案是构建一个自定义 VideoView 类型类,以便我可以访问其 MediaPlayer 实例。
有什么想法吗?感谢您的阅读。
Does anyone know if it's possible to detect when a VideoView is buffering?
I want to show a ProgressDialog when the video is buffering.
So far I tried using a OnPreparedListener, but that only works when the video is first loaded. If a video is playing and the user moves the scrub bar to a different point the video is still "prepared" even though it is buffering.
I also tried (I know this is awful) an AsyncThread that just busy waits on isPlaying():
private class BufferTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...voids) {
final VideoView videoView = (VideoView)findViewById(R.id.video);
while (!videoView.isPlaying()) { }
return null;
}
protected void onPostExecute(Void v) {
// Hide the dialog here...
}
}
This doesn't work because as soon as you call start() a VideoView seems to be considered playing even though it is buffering.
The only solution I can think of is building a custom VideoView type class so I can access its MediaPlayer instance.
Any ideas? Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
VideoView 在缓冲时显示进度。
下面的代码对我有用:
VideoView showing Progress while Buffering.
Below code worked for me:
我正在研究类似的事情,但无法想出一个很好的解决方案。 此处发布了一些有趣的解决方案,您应该检查一下是否你还没见过他们。
不管怎样,我想出了以下在上面的线程中暗示的黑客方法,目前工作正常。
I'm working on something similar, and couldn't come up with a great solution. Some interesting solutions were posted here that you should check out if you haven't seen them.
Anyway, I came up with the following hack that was hinted at in the above thread and works ok for now.
这对我有用
This worked for me
自 API 级别 17,您现在可以从 MediaPlayer 访问 InfoListener:
Since API level 17, you can now access the InfoListener from the MediaPlayer:
为了不实现自定义 VideoView,我使用了以下 hack。这个想法是每 1 秒检查一次当前位置是否与前 1 秒相同。如果是,则视频正在缓冲。如果没有,则视频确实正在播放。
videoMessage 只是一个 TextView,文本“Buffering...”放置在我的 VideoView 的中心。
I came with the following hack in order to not implement a custom VideoView. The idea is to check every 1 second if the current position is the same as 1 second before. If it is, the video is buffering. If not, the video is really playing.
videoMessage is just a TextView with the text "Buffering..." placed in the center of my VideoView.
每次 VideoView 缓冲时,以下代码都会显示一个缓冲对话框。
Following code will show a buffering dialog every time the VideoView is buffering.