Android - 播放完成后无法隐藏 VideoView

标签 android android-videoview android-relativelayout

我在 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);
}

关于Android - 播放完成后无法隐藏 VideoView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075686/

相关文章:

java - 如何在android中安排RecyclerView上的数据?

Android - RelativeLayout 中的 ImageButton 与父尺寸不匹配

android - 在没有 onClickListener 的情况下在日历 View 中突出显示特定日期

javascript - Window.open + 移动设备 + Canvas == 不工作?

Android检测来自URL的视频是否为纵向

android - webview 是否像 videoview 一样创建一个单独的表面?

android - 在线为Android应用程序VideoView存储视频

android - 带有自定义可绘制缩放比例的内容插入

android - 防止 RelativeLayout 环绕背景图像

Android 上的 JavascriptInterface 不能在 Release模式下使用 APK