Giuseppe Pennisi Giuseppe Pennisi - 12 days ago 6
Android Question

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" />



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