sku
sku

Reputation: 605

How to know duration of any media file

My application shows list of media(audio and Video) files in the sdcard along with file size,mime type. I now want to add total duration of the media file in the every list item. I tried fetching the duration using following code

String[] proj = { MediaStore.Video.VideoColumns.DURATION };
 Cursor cursor = managedQuery(contentUri, proj, null, null, null);
 int column_index
  = cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION);

but MediaStore returns correct duration only if the media is played before, if not it returns 0. I cannot use videoview.getDuration since I don't want to play the video. I want your help to know if I am making any mistake in above code or if there is any other way to fetch duration without playing it first.

Upvotes: 1

Views: 1232

Answers (1)

Dennis Wronka
Dennis Wronka

Reputation: 88

You should be able to use MediaMetadataRetriever to read the duration from the file. But using this class will require your project to be at least API level 10 (Android 2.3.3).

Upvotes: 2

Related Questions