How to display song duration

So, I use Mediastore to get the length of the song, but I need to convert them to Seconds I need to format them to HH: MM: SS (Hours: minutes: seconds) I tried using TimeUnit but it didn’t work, or maybe I didn’t implement it correctly way.

Thanks for any help. Sorry for the bad English.

Please, help. SongAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {




    TextView title1 = (TextView) view.findViewById(R.id.titlelist);
    TextView artist1 = (TextView) view.findViewById(R.id.artistlist);
    TextView duration1 = (TextView) view.findViewById(R.id.durationlist);
    ImageView album1 = (ImageView) view.findViewById(R.id.iconlist);

     title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
    String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
    String duration = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
    String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
    long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
      StringBuilder titleBuild = new StringBuilder();

      titleBuild.append(title);

      if(titleBuild.length() > 35)
      {
      titleBuild.setLength(32);
      title = titleBuild.toString()+"...";
      }
      else
      {
          title = titleBuild.toString();
      }
      StringBuilder artistBuild = new StringBuilder();
      artistBuild.append(artist);
      if(artistBuild.length() > 35)
      {
      artistBuild.setLength(32);
      artist = artistBuild.toString()+"...";
      }
      else
      {
      artist = artistBuild.toString();
      }

      final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);

        if(albumArtUri == null){

            Picasso.with(context).load(R.drawable.default_artwork)
            .resize(300, 300).centerInside().into(album1);

        }


          Picasso.with(context).load(albumArtUri)
          .resize(300, 300).centerInside().into(album1);



duration1.setText(duration);          
title1.setText(title);
artist1.setText(artist);
}
+4
source share
2 answers

Once you have the duration,

call this procedure (but I'm sure there are more elegant ways).

public String convertDuration(long duration) {
    String out = null;
    long hours=0;
    try {
        hours = (duration / 3600000);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return out;
    }
    long remaining_minutes = (duration - (hours * 3600000)) / 60000;
    String minutes = String.valueOf(remaining_minutes);
    if (minutes.equals(0)) {
        minutes = "00";
    }
    long remaining_seconds = (duration - (hours * 3600000) - (remaining_minutes * 60000));
    String seconds = String.valueOf(remaining_seconds);
    if (seconds.length() < 2) {
        seconds = "00";
    } else {
        seconds = seconds.substring(0, 2);
    }

    if (hours > 0) {
        out = hours + ":" + minutes + ":" + seconds;
    } else {
        out = minutes + ":" + seconds;
    }

    return out;

}
+8
source

Theo's answer is really good, it affects the display of time in two states:

0, .

, , , .

, :

//convert the song duration into string reading hours, mins seconds
int dur = (int) song.get(position).getDuration();

int hrs = (dur / 3600000);
int mns = (dur / 60000) % 60000;
int scs = dur % 60000 / 1000;

String songTime = String.format("%02d:%02d:%02d", hrs,  mns, scs);

:

  • ; 1000 = 1
  • % aka, 60 000 dur?
  • 1000 scs - , .

, , :

//convert the song duration into string reading hours, mins seconds
int dur = (int) song.get(position).getDuration();

int hrs = (dur / 3600000);
int mns = (dur / 60000) % 60000;
int scs = dur % 60000;

NumberFormat formatter = new DecimalFormat("00");
String seconds = formatter.format(scs);

String songTime = String.format("%02d:%02d", hrs,  mns);
String output = songTime + ":" + seconds;

, , Java , 3- - , , , , % 02d, scs 1000. , , 00.

+1

All Articles