1

I want to get the length of a saved video from my device. I have tried with MediaMetadataRetriever but my application crashes. I am testing it on Android 6.0 but it is not working at all.

Below is my Activity.

    public class MainActivity extends AppCompatActivity {

    private static final int PICK_IMAGE_REQUEST = 1; // put it on the top.
    private Button btnOpenGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnOpenGallery = (Button)findViewById(R.id.btnOpenGallery);
        btnOpenGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            Uri selectedImageUri = data.getData();
            String strPath = selectedImageUri.getPath();
            Log.e("Path is ","===>"+strPath);


            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(strPath); // Enter Full File Path Here
            String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            long timeInmillisec = Long.parseLong(time);
            Log.e("Media duration in ","==>"+timeInmillisec);

        }
    }
}
Mohd Sakib Syed
  • 1,679
  • 1
  • 25
  • 42

2 Answers2

0

try this one

MediaPlayer mp = MediaPlayer.create(getActivity(), Uri.parse(Environment.getExternalStorageDirectory() + "/Downloads/" + "vid" + ".mp4")); // Downloads is the folder and vid is video file.
int duration = mp.getDuration();
mp.release();

This was worked for me.

Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
arshad shaikh
  • 673
  • 8
  • 11
  • I tried your suggestion but app get crash and show error `java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getDuration()' on a null object reference` – Mohd Sakib Syed Mar 30 '17 at 11:45
0

Try this:

    MediaPlayer mp = MediaPlayer.create(getActivity(), Uri.parse(Environment.getExternalStorageDirectory() + "/Downloads/" + "vid" + ".mp4")); // Downloads is the folder and vid is video file.
    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            int duration = mp.getDuration();
            mp.release();
        }
    });
Anton111111
  • 656
  • 1
  • 7
  • 23