Get mp3 duration in android

18,286

Solution 1

You can use the MediaMetadataRetriever to get the duration of a song. Use the METADATA_KEY_DURATION in combination with the extractMetadata() funciton.

Solution 2

       // load data file
       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();

Solution 3

Here is the Kotlin version:

var metaRetriever:MediaMetadataRetriever = MediaMetadataRetriever()
metaRetriever.setDataSource(filePath)

var out:String = ""
var txtTime:String = ""

var duration:String = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)

Log.d("DURATION VALUE", duration)

var dur:Long = duration.toLong()
var seconds:String = ((dur % 60000)/1000).toString()

Log.d("SECONDS VALUE", seconds)

var minutes:String = (dur / 60000).toString()
out = minutes + ":" + seconds

if (seconds.length == 1){
    txtTime = "0" + minutes + ":0" + seconds
}
else {
    txtTime = "0" + minutes + ":" + seconds
}

Log.d("MINUTES VALUE", minutes)
Log.d("FORMATTED TIME", txtTime)

metaRetriever.release()

Solution 4

If you want to support older Android versions, you can use a 3rd party library. For example http://www.jthink.net/jaudiotagger/ works fine, though it's relatively space consuming for an Android application (a little less than 1 MB).

True programmers would of course parse the duration from the binary file without using any libraries ;) I didn't have enough skill for this.

Share:
18,286
pleerock
Author by

pleerock

Typescript/Javascript backend (node.js) and frontend (angular) developer. Previously PHP (symfony) & Java (android) developer. Over 10 years in software development. Checkout my open source repositories on github.

Updated on July 30, 2022

Comments

  • pleerock
    pleerock almost 2 years

    How to get mp3 track duration without creating MediaPlayer instance? I just need to show mp3 song length in mp3 file list, so I think that I shouldn't create MediaPlayer object for each of tracks in the list

    And another:

    sometimes MediaPlayer returns wrong duration of the song ( I think its so because bitrate of those files is dinamic ). How can I get right duration of the song?

  • pleerock
    pleerock over 12 years
    Thank you very much, this class is very helpful, but its working from api level 10, android 2.3.3 and higher. I need more universal solution(
  • Kurtis Nusbaum
    Kurtis Nusbaum over 12 years
    Hmmm, sorry. I don't know of any other way.
  • miq
    miq over 12 years
    This isn't very useful since most of the people still have Eclair or Froyo. Maybe in a few years..
  • Kurtis Nusbaum
    Kurtis Nusbaum over 12 years
    @miq Do you have an alternative solution?
  • Pulkit Goyal
    Pulkit Goyal about 11 years
    jaudiotagger is not compatible with Android.
  • TheRealChx101
    TheRealChx101 over 4 years
    @miq Last time I looked at the source code there were Swing imports