So Im Using Mediastore to get the Songs Duration 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 didnt work or maybe i didnt implement it the correct way
Thank You For Any Help Sorry For 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);
}
Once you have the duration,
call this routine (but I am 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;
}
Theo's answer is really good, it factors in displaying the time in two states:
If hours is not greater than 0, only display the minutes and seconds.
It is quite a tidy approach, but as he mentioned, it's not very elegant.
You can simplify things right down by using this:
//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);
There are three things you should know here:
%
is used to get the remainder of a sum aka how many times does 60,000 fit into the value dur
?scs
is to get the absolute value in seconds, not milliseconds. If you don't wish to use this, perhaps you'd prefer to use:
//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;
From what I understand, Java does not like Milliseconds being any less than 3 digits due to the fact it's so small and thus doesn't display kindly using %02d, which is why I divided scs
by 1000. Using the Numberformatter forces formatting of a number value to only be as long you assign, which in this case is 00
.
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