简体   繁体   中英

Make videoview visible only when video is played

i'm trying to display a videoview only when the video is played. When i click on an item of gridview the video starts and videoview is visible, but when the video stops, it doesn't desappear. Here the code: Main 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());


        }

Here the layout:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM