使 videoview 仅在播放视频时可见

Make videoview visible only when video is played

我试图仅在播放视频时显示 videoview。 当我单击 gridview 的项目时,视频开始并且 videoview 可见,但是当视频停止时,它不会消失。这里的代码: 主要 Activity:

public class MainActivity extends AppCompatActivity {
public Integer[] mVideo = {
        R.raw.clesella, R.raw.simonino
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new VideoAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final VideoView mVideoView2 = (VideoView) findViewById(R.id.video);
            // VideoView mVideoView = new VideoView(this);
            String uriPath = "android.resource://"+ getPackageName()+"/"+mVideo[position];
            Uri uri2 = Uri.parse(uriPath);
            mVideoView2.setVideoURI(uri2);
            mVideoView2.requestFocus();
            mVideoView2.start();
            mVideoView2.postDelayed(new Runnable() {
                public void run() {

                    mVideoView2.setVisibility(View.VISIBLE);

                }
            }, mVideoView2.getDuration());


        }

布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.graduation.nakama.harrygraduation.MainActivity">


<VideoView
    android:id="@+id/video"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:visibility="invisible"
    android:layout_alignParentStart="true"
    android:layout_marginTop="100dp" />

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"

    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_centerHorizontal="true" />

mVideoView2.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub

       //write your code after complete video play  
       mVideoView2.setVisibility(View.GONE);
    }
});