4

I am trying to implement a simple VideoView that would play the Video from the file system. The Video loads fine and plays just fine. However, I would like to know when the Video finished playing so that I can finish() the activity and resume with the next. Here is what I have.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videoPlayer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

The Activity is as below.

    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoplayer);

    videoView = (VideoView) findViewById(R.id.videoView);
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
    {
        @Override
        public void onCompletion(MediaPlayer mp)
        {
            Intent result = new Intent();
            setResult(1, result);
            finish();
        }
    }); // video finish listener

    Intent intent = getIntent();

    // Receiving the Data
    String fileName = intent.getStringExtra("fileName");

    videoView.setVideoPath(fileName);
    videoView.setMediaController(new MediaController(this));
    videoView.requestFocus();
    videoView.start();}

I call this Activity from a different Activity. Here is that Activity definition.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:keepScreenOn="true">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/imageViewDescription" />
    </LinearLayout>

VideoPlayer Activity Class

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.imageplayer);

    imageView = (ImageView) findViewById(R.id.imageView);

    updateUI(new File("/sdcard/Test/Test.3gp");
}

public void updateUI(File file)
{
        if (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png"))
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

            imageView.setImageBitmap(myBitmap);
        }
        else if (file.getName().toLowerCase().endsWith(".3gp") || file.getName().toLowerCase().endsWith(".mp4"))
        {
            //Starting a new Intent
            Intent nextScreen = new Intent(getApplicationContext(), VideoPlayer.class);

            //Sending data to another Activity
            nextScreen.putExtra("fileName", file.getAbsolutePath());

            released = false;

            startActivityForResult(nextScreen, resultCode);
        }
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
}

The onCompletion method is never called and the onActivityResult() method in the caller of this Activity also is not called. I would like to know what is wrong with this. I tried playing both '.3gp' and '.mp4' files with the same result. Please help

2 Answers 2

3

call videoView.start() in onPreparedListener and set mediaplayer object looping with false value. I hope this will work.. Do like as follow:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.setLooping(false); //make sure to use this statement
                    video.start();
                    Toast.makeText(mContext,String.valueOf(vtime),Toast.LENGTH_LONG).show();
               }
     });
1
  • It loops by default AND it doesn't complete when looped. Thank you!
    – John Glen
    Oct 11, 2020 at 2:31
1

Well, since I did not get any response here, I tried many different approaches and finally had some luck with this. Instead of having the Image Viewer Activity call the Video Player activity, I created a blank activity that does not have any UI. I iterate over the files in this activity and then call either the Image Viewer Activity or the Video Player activity by passing in the File name as a parameter in the Intent. This is working fine and no issues. Hope someone answers as to why that previous implementation did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.