How to get length in milliseconds of video from URL without video view in Android?-ThrowExceptions

Exception or error:

I am making a Media player in Android. I require a code to get length of video without using a video view.

I saw many pages in Stack overflow but ever page used to show how to get length of a video in video view but i require without video view.

How to solve:

You could use the MediaMetadataRetriever to get such an information. This class is meant to be used by users who wants to get specific informations about a media without having to use the MediaPlayer.

Here’s a sample of how to use it :

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(your_data_source);
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong( time );
long duration = timeInmillisec / 1000;
long hours = duration / 3600;
long minutes = (duration - hours * 3600) / 60;
long seconds = duration - (hours * 3600 + minutes * 60);

Leave a Reply

Your email address will not be published. Required fields are marked *