Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Created August 18, 2014 09:08
Show Gist options
  • Select an option

  • Save XinyueZ/87ea8d5a5eb5510546ee to your computer and use it in GitHub Desktop.

Select an option

Save XinyueZ/87ea8d5a5eb5510546ee to your computer and use it in GitHub Desktop.
Get duration of a remote video.
/**
* Asyn-post to get duration of video.
*
* @param _context
* {@link android.content.Context}.
* @param _urlToVideo
* The url to video in {@link java.lang.String}.
*
* @return {@link java.lang.String}. The Duration of video in long format like {@code 03:45}.
* <p/>
* Null might be returned if unsuccessful since some reasons like with offline status try to get a remote video.
*/
public static String getVideoDuration(Context _context, String _urlToVideo) {
String dur = null;
try {
MediaPlayer mp = MediaPlayer.create(_context, Uri.parse(_urlToVideo));
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
NumberFormat f = new DecimalFormat("##00");
dur = String.format("%s:%s",
f.format(TimeUnit.MILLISECONDS.toMinutes(duration)),
f.format(TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))));
} catch (Exception _e) {
LL.error("Can't get duration of video, checkout internet connection.");
}
return dur;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment