VideoView 在不可见时不会启动

发布于 2024-12-18 10:40:37 字数 1198 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-12-25 10:40:37

VideoView 实际上是一个专门的 SurfaceView。 SurfaceView 的工作原理是在普通窗口(包含所有视图)后面创建另一个窗口,然后有一个透明区域,以便可以在其后面看到新窗口(具有自己的绘图表面)。

如果 SurfaceView 不再可见,则其表面将被销毁,即调用 SurfaceHolder.Callback.surfaceDestroyed 。如果没有有效的表面,VideoView 将不会尝试播放其视频,因此您的 AsyncTask 将永远不会离开 doInBackground

当 SurfaceView 的窗口可见时,Surface 将会为您创建;您应该实现 surfaceCreated(SurfaceHolder) 和 surfaceDestroyed(SurfaceHolder) 来发现在窗口显示和隐藏时 Surface 何时被创建和销毁。

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. The VideoView will not try to play its video if there is not a valid surface, hence your AsyncTask will be able to never leave doInBackground.

The Surface will be created for you while the SurfaceView's window is visible; you should implement surfaceCreated(SurfaceHolder) and surfaceDestroyed(SurfaceHolder) to discover when the Surface is created and destroyed as the window is shown and hidden.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文