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.
By using METADATA_KEY_DURATION constant of FFmpegMediaMetadataRetriever you can get the duration of your video.It will return the string to you then you can convert it into LONG to get TIME. MediaMetadataRetriever retriever = new MediaMetadataRetriever(); if (Build. VERSION.
↳ java.time.Duration. A time-based amount of time, such as '34.5 seconds'. This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With