Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Duration of captured video in android?

Tags:

android

Using below code i captured Video,but how to get duration of captured video in android ? and also when sometimes user discard video and record new video then get duration of new video .

Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, TAKE_VIDEO);
like image 928
MAC Avatar asked Dec 04 '22 03:12

MAC


2 Answers

I think the easiest way is:

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile);
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
return String.format("%d min, %d sec", 
        TimeUnit.MILLISECONDS.toMinutes(duration),
        TimeUnit.MILLISECONDS.toSeconds(duration) - 
        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
    );
like image 185
Nolesh Avatar answered Jan 03 '23 15:01

Nolesh


Using cursor you can get duration and there are duration Column in Cursor and you can get as a string.

Cursor cursor = MediaStore.Video.query(getContentResolver(),data.getData(),
                new String[] { MediaStore.Video.VideoColumns.DURATION });
System.out.println(">>>>>>>>>>"+cursor.getCount());
cursor.moveToFirst();

String duration = cursor.getString(cursor.getColumnIndex("duration"));
like image 38
Samir Mangroliya Avatar answered Jan 03 '23 15:01

Samir Mangroliya