I\'m a beginner in Android programming.
I\'m writing an application to list all video file in a folder and display information of all videos in the folder. But when
Here is the way to fetch media file duration in Kotlin
fun File.getMediaDuration(context: Context): Long {
if (!exists()) return 0
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, Uri.parse(absolutePath))
val duration = retriever.extractMetadata(METADATA_KEY_DURATION)
retriever.release()
return duration.toLongOrNull() ?: 0
}
If you want to make it safer (Uri.parse could throw exception), use this combination. The others are generally just useful as well :)
fun String?.asUri(): Uri? {
try {
return Uri.parse(this)
} catch (e: Exception) {
}
return null
}
val File.uri get() = this.absolutePath.asUri()
fun File.getMediaDuration(context: Context): Long {
if (!exists()) return 0
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, uri)
val duration = retriever.extractMetadata(METADATA_KEY_DURATION)
retriever.release()
return duration.toLongOrNull() ?: 0
}
Not necessary here, but generally helpful additional Uri extensions
val Uri?.exists get() = if (this == null) false else asFile().exists()
fun Uri.asFile(): File = File(toString())
public static long getDurationOfSound(Context context, Object soundFile)
{
int millis = 0;
MediaPlayer mp = new MediaPlayer();
try
{
Class<? extends Object> currentArgClass = soundFile.getClass();
if(currentArgClass == Integer.class)
{
AssetFileDescriptor afd = context.getResources().openRawResourceFd((Integer)soundFile);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
}
else if(currentArgClass == String.class)
{
mp.setDataSource((String)soundFile);
}
else if(currentArgClass == File.class)
{
mp.setDataSource(((File)soundFile).getAbsolutePath());
}
mp.prepare();
millis = mp.getDuration();
}
catch(Exception e)
{
// Logger.e(e.toString());
}
finally
{
mp.release();
mp = null;
}
return millis;
}
MediaPlayer mpl = MediaPlayer.create(this,R.raw.videoFile);
int si = mpl.getDuration();
This will give the duration of the video file
Use MediaMetadataRetriever
to retrieve media specific data:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(context, Uri.fromFile(videoFile));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time );
retriever.release()
I think the easiest way is:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile));
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
return String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uriOfFile);
long duration = Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();