Usually, Android VideoView is does not show video, audio is playing. How can I get the video to show 100% of the time

梦想与她 提交于 2019-12-11 02:21:33

问题


I am using Mono for Android.

I've poured through all the posts I can find concerning the use of VideoView and can't find an answer. And I have switched to using a SurfaceView with a MediaPlayer and back again with no luck.

I want a video to start playing after the user has not touched the screen for a while, so I have a FrameLayout that holds the VideoView, and I make that visible when I want the video to show up.

When I start the video, it usually doesn't show up (about 80% of the time), but the audio starts playing. If you touch the screen, the video shows up. I've tried different videos (.mp4 and .3gp), but since the video is actually displaying after you touch the screen, I suspect it is something else.

I have tried this on a couple devices with the same result, albeit they are from from the same manufacturer. They are both Coby devices (one 7" and the other 9.7") and are running Android 4.0. I am targeting Android 2.3 API.

here is my xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    ... A bunch of controls ...

<FrameLayout
        android:id="@+id/videoViewBack"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF000000"
        android:visibility="gone">
    <VideoView android:id="@+id/videoView"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_gravity="center"
              android:visibility="visible"/>
</FrameLayout>
</LinearLayout>

and my function to start the video looks like this:

        private void StartVideo()
    {

        RequestedOrientation = ScreenOrientation.SensorLandscape; // Force into landscape so the video looks good

        FrameLayout vidBack = FindViewById<FrameLayout>(Resource.Id.videoViewBack);
        vidBack.Visibility = ViewStates.Visible;

        VideoView vid = FindViewById<VideoView>(Resource.Id.videoView);
        vid.SetOnTouchListener(this);
        vid.SetOnCompletionListener(this);
        vid.SetVideoPath(_attractFile);
        vid.SetMediaController(new MediaController(this));
        vid.RequestFocus();
        vid.Start();
    }

I have also tried to place the Start() call in an OnPreparedListener but that didn't help.

I am pulling out my hair, and hopefully somebody knows what dumb thing I am forgetting to do.

EDIT:

On the theory that the problem was related to the video not being sized correctly in the layout pass, I did the following which seems to have corrected the issue. But I'm not 100% sure why, or if it is just poking jello

In the OnPrepared handler I added:

View mainScreen = FindViewById<View>(Resource.Id.MainScreen);
mainScreen.requestLayout();
mainScreen.postInvalidateDelayed(1000);
mainScreen.postInvalidate();

where mainScreen is the main FrameLayout in my xml. Doing the PostInvalidate fixed it most of the time, adding the PostInvalidateDelayed fixed it 100% of the time (I think :) ).

Thoughts as to why this works?


回答1:


    @Override
public void surfaceCreated(final SurfaceHolder holder) {
    mPlayer.setDisplay(holder); // 指定SurfaceHolder

    gameActivity.handler.post(new Runnable() {
        @Override
        public void run() {
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e1) {
            } catch (IOException e1) {
            }
        }
    });
}



回答2:


Try this:

videoView.setZOrderOnTop(true);


来源:https://stackoverflow.com/questions/11805205/usually-android-videoview-is-does-not-show-video-audio-is-playing-how-can-i-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!