How to get duration of MP3 file in Android
I am working on a media player project and I want to rotate the image according to the length of the MP3 file I am playing, i.e. the image should stop rotating when the song is finished. I want to get the duration of the selected MP3 file so I can rotate time.
I have read this question Get PCM data from MP3 file in Android , but I could not see how to read song duration. How can I do this - is there any function or methods to get the duration of the file?
+3
source to share
1 answer
Hope this helps you
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(filePath);
String out = "";
// get mp3 info
// convert duration to minute:seconds
String duration =
metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Log.v("time", duration);
long dur = Long.parseLong(duration);
String seconds = String.valueOf((dur % 60000) / 1000);
Log.v("seconds", seconds);
String minutes = String.valueOf(dur / 60000);
out = minutes + ":" + seconds;
if (seconds.length() == 1) {
txtTime.setText("0" + minutes + ":0" + seconds);
}else {
txtTime.setText("0" + minutes + ":" + seconds);
}
Log.v("minutes", minutes);
// close object
metaRetriever.release();
+7
source to share