VideoView 在不可见时不会启动
我有一个 AsyncTask,在其中隐藏视频视图,开始视频播放,并在视频播放时显示视频视图。
但是,当视频视图设置为不可见时,视频将不会启动,异步任务会一直挂在 onBackground 中。如果我注释掉这一行,视频就会开始播放。 为什么视频视图需要可见表面?
public void walk(final View v) {
new AsyncTask() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mVideoView.setVisibility(View.INVISIBLE); // this line causes video not to start
mVideoView.start();
}
@Override
protected Object doInBackground(Object... objects) {
while (!mVideoView.isPlaying()) {}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
mVideoView.setVisibility(View.VISIBLE);
}
}.execute();
我这样做的一些背景知识:我尝试避免启动视频时通常遇到的众所周知的黑色闪光问题:
https://stackoverflow.com/search?q=%5Bandroid%5D+videoview+black
https://stackoverflow.com/search?q=%5Bandroid%5D+video+%5Bmediaplayer% 5D+黑色
I have an AsyncTask, where I hide a video view, start the video playback, and show the video view when the video is playing.
But the video would just not start when the video view is set to invisible, the async task keeps hanging in onBackground. If I comment out this line, the video starts playing.
Why does the video view require a visible surface?
public void walk(final View v) {
new AsyncTask() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mVideoView.setVisibility(View.INVISIBLE); // this line causes video not to start
mVideoView.start();
}
@Override
protected Object doInBackground(Object... objects) {
while (!mVideoView.isPlaying()) {}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
mVideoView.setVisibility(View.VISIBLE);
}
}.execute();
A bit of background why I'm doing this: I try to avoid the well-known issue of the black flash that you usually have when starting a video:
https://stackoverflow.com/search?q=%5Bandroid%5D+videoview+black
https://stackoverflow.com/search?q=%5Bandroid%5D+video+%5Bmediaplayer%5D+black
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VideoView
实际上是一个专门的 SurfaceView。 SurfaceView 的工作原理是在普通窗口(包含所有视图)后面创建另一个窗口,然后有一个透明区域,以便可以在其后面看到新窗口(具有自己的绘图表面)。如果 SurfaceView 不再可见,则其表面将被销毁,即调用 SurfaceHolder.Callback.surfaceDestroyed 。如果没有有效的表面,
VideoView
将不会尝试播放其视频,因此您的AsyncTask
将永远不会离开doInBackground
。The
VideoView
is really a specialised SurfaceView. A SurfaceView works by creating another window behind the normal window (containing all of the views), and then having an area of transparency so that the new window (with its own drawing surface) can be seen behind it.If a SurfaceView is no longer visible, its surface will be destroyed i.e.
SurfaceHolder.Callback.surfaceDestroyed
is called. TheVideoView
will not try to play its video if there is not a valid surface, hence yourAsyncTask
will be able to never leavedoInBackground
.