Android examples for android.media:Video
Get runtime duration of media such as audio or video in milliseconds
import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore.Video; import android.util.Log; public class Main { private static final String TAG = ""; /**/*w w w. jav a 2 s .c om*/ * Get runtime duration of media such as audio or video in milliseconds ****/ public static long getDuration(Context ctx, Uri mediaUri) { Cursor cur = ctx.getContentResolver().query(mediaUri, new String[] { Video.Media.DURATION }, null, null, null); long duration = -1; try { if (cur != null && cur.getCount() > 0) { while (cur.moveToNext()) { duration = cur.getLong(cur.getColumnIndex(Video.Media.DURATION)); if (duration == 0) Log.w(TAG, "#getMediaDuration The image size was found to be 0. Reason: UNKNOWN"); } // end while } else if (cur.getCount() == 0) { Log.e(TAG, "#getMediaDuration cur size is 0. File may not exist"); } else { Log.e(TAG, "#getMediaDuration cur is null"); } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } return duration; } }