3

I am working on Video file duration finding in android. But i am unable to get it . My video files are available in the particular folder in the SD-card. Want to bind them as the list view with the duration and name . I have got the name . But searching solution to find the duration . Please help me . Thanks in advance .

Rajesh .

RAJESH
  • 231
  • 1
  • 5
  • 19

3 Answers3

10

int msec = MediaPlayer.create(context, Uri.fromFile(new File(path))).getDuration();

sergey.n
  • 1,672
  • 19
  • 18
  • 5
    don't create MediaPlayer instance just to get file length. There is a dedicated MediaMetadataRetriever class in android. More details available here: http://stackoverflow.com/a/33247728/1713920 – vir us Mar 16 '16 at 17:44
2

You don't need to create MediaPlayer. I have made one function which will give you duration of your video file stored android device.

public static long checkVideoDurationValidation(Context context, Uri uri){
    Log.d("CommonHandler", "Uri: " + uri);
    Cursor cursor = MediaStore.Video.query(context.getContentResolver(), uri, new
            String[]{MediaStore.Video.VideoColumns.DURATION});
    long duration = 0;
    if (cursor != null && cursor.moveToFirst()) {
        duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video
                .VideoColumns.DURATION));
        cursor.close();
    }

    return duration;
}

Let me know if you have any doubt in this.

Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • 1
    Even if I pass a URI, I am always getting cursor as null (Uri has been passed correctly) Any suggestions? – prateek31 Jul 22 '18 at 13:28
0

Don't use MediaPlayer! It is inefficient

Use MediaMetadataRetriever instead to get only the meta data that you need

Java Solution

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// There are other variations of setDataSource(), if you have a different input
retriever.setDataSource(context, Uri.fromFile(videoFile));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long durationMs = Long.parseLong(time);
retriever.release()

Kotlin Extension Solution

Here is the way to fetch media file duration in Kotlin

fun File.getMediaDuration(context: Context): Long {
    if (!exists()) return 0
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(context, Uri.parse(absolutePath))
    val duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
    retriever.release()

    return duration.toLongOrNull() ?: 0
}

If you want to make it safer (Uri.parse could throw exception), use this combination. The others are generally just useful as well :)

fun String?.asUri(): Uri? {
    try {
        return Uri.parse(this)
    } catch (e: Exception) {
    }
    return null
}

val File.uri get() = this.absolutePath.asUri()

fun File.getMediaDuration(context: Context): Long {
    if (!exists()) return 0
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(context, uri)
    val duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
    retriever.release()

    return duration.toLongOrNull() ?: 0
}

Not necessary here, but generally helpful additional Uri extensions

val Uri?.exists get() = if (this == null) false else asFile().exists()

fun Uri.asFile(): File = File(toString())
Gibolt
  • 42,564
  • 15
  • 187
  • 127