我在RelativeLayout中有一个VideoView,它位于WebView的顶部。我想要的是在视频播放完毕后隐藏视频以显示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();
}
任何帮助都会受到极大的欢迎!
答案 0 :(得分:2)
你需要这样的东西。抱歉格式化。
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);
}