所以我使用Mediastore获取歌曲持续时间,但我需要将它们转换为秒 我需要将它们格式化为HH:MM:SS(小时:分钟:秒) 我尝试使用TimeUnit但是没有工作或者我没有以正确的方式实现它
感谢您的帮助 对不起英文不好
请帮助。 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);
}
答案 0 :(得分:8)
一旦你有持续时间,
调用此例程(但我确信有更优雅的方法)。
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;
}
答案 1 :(得分:1)
Theo的回答非常好,它决定了在两种状态下显示时间:
如果小时不大于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);
这里你应该知道三件事:
%
来获取总和的剩余部分,即60,000符合值dur
的次数?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不喜欢Milliseconds不到3位数,因为它很小,因此没有使用%02d显示,这就是为什么我划分{{使用Numberformatter强制将数字值格式化为您指定的时间(在本例中为scs
)。
答案 2 :(得分:0)
从MediaPlayer
或MediaStore
课上获取时间
按以下步骤设置文本(在Kotlin中)(在Java中使用setText)
songTimeFav?.text = String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(finalTime?.toLong() as Long),
TimeUnit.MILLISECONDS.toSeconds(finalTime?.toLong() as Long) -
TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(finalTime.toLong())))
songTimeFav是TextView的对象