Android - 播放结束后无法隐藏 VideoView

Android - Can't hide VideoView after playback finishes

我在 WebView 顶部的 RelativeLayout 中有一个 VideoView。我只想在视频结束后隐藏视频以显示 WebView。

我的问题是,我最初可以在 onCreate 中隐藏 VideoView(父布局),然后我可以在 onPrepared 中显示视频,但是一旦视频结束我就无法再次隐藏它。

如果我最初不隐藏视频并在屏幕上保持打开状态,那么它会在 onCompletion 中被告知这样做时隐藏。它不会在下一次重新显示,就像第一部分以某种方式阻止了第二部分。

onCompletion 和 onPrepared 总是被调用,这些函数中的其他函数工作得很好。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp" tools:context=".MainActivity">

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/videoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp" tools:context=".MainActivity">

    <VideoView
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/videoView" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity  implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {

private RelativeLayout videoLayout;
private VideoView videoHolder;

MediaPlayer vp = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    videoLayout = (RelativeLayout) findViewById(R.id.videoLayout);
    videoHolder = (VideoView) findViewById(R.id.videoView);

    videoLayout.setVisibility(RelativeLayout.GONE);
}


public void videoPlayer() {
    if(videosCanPlay>0) {
        try {
            Thread.sleep(delayVideo);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        video = file_name;
        videoHolder.setVideoPath(video);

        videoHolder.setOnPreparedListener(this);
        videoHolder.setOnCompletionListener(this);

    } else {
        Log.i("PLAYLIST","Playlist Can't Play Yet");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        videoPlayer();
    }
}

public void hideVideo() {
    videoLayout.setVisibility(RelativeLayout.GONE);
}

public void showVideo() {
    videoLayout.setVisibility(RelativeLayout.VISIBLE);
}

@Override
public void onPrepared(MediaPlayer vp) {
    videoHolder.start();
    showVideo();
    duration = videoHolder.getDuration();
}

@Override
public void onCompletion(MediaPlayer vp) {
    videoHolder.stopPlayback();
    hideVideo();
    videoPlayer();
}

如有帮助,将不胜感激!

你需要这样的东西。抱歉格式化。

public void videoPlayer() {
    if(videosCanPlay>0) {
        video = file_name;
        videoHolder.setVideoPath(video);

        videoHolder.setOnPreparedListener(this);
        videoHolder.setOnCompletionListener(this);

    } else {
        Log.i("PLAYLIST","Playlist Can't Play Yet");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                videoPlayer();
            }
        }, 1000);
    }
}

@Override
public void onCompletion(MediaPlayer vp) {
    videoHolder.stopPlayback();
    hideVideo();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            videoPlayer();
        }
    }, delayVideo);
}