音樂樣本應用程序有一個類MusicUtils:
/* Try to use String.format() as little as possible, because it creates a
* new Formatter every time you call it, which is very inefficient.
* Reusing an existing Formatter more than tripled the speed of
* makeTimeString().
* This Formatter/StringBuilder are also used by makeAlbumSongsLabel()
*/
private static StringBuilder sFormatBuilder = new StringBuilder();
private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
private static final Object[] sTimeArgs = new Object[5];
public static String makeTimeString(Context context, long secs) {
String durationformat = context.getString(
secs < 3600 ? R.string.durationformatshort : R.string.durationformatlong);
/* Provide multiple arguments so the format can be changed easily
* by modifying the xml.
*/
sFormatBuilder.setLength(0);
final Object[] timeArgs = sTimeArgs;
timeArgs[0] = secs/3600;
timeArgs[1] = secs/60;
timeArgs[2] = (secs/60) % 60;
timeArgs[3] = secs;
timeArgs[4] = secs % 60;
return sFormatter.format(durationformat, timeArgs).toString();
}
,您可以使用。
但是,如果您將getCurrentPosition()的結果除以1000,carlovv的解決方案也可以工作,因爲該方法返回毫秒。
這是在strings.xml
<string translatable="false" name="durationformatshort">
<xliff:g id="format">%2$d:%5$02d</xliff:g>
</string>
<string translatable="false" name="durationformatlong">
<xliff:g id="format">%1$d:%3$02d:%5$02d</xliff:g>
</string>
得到了另一篇文章的答案。看看這個鏈接的答案。 http://stackoverflow.com/questions/5548922/how-do-i-correctly-display-the-position-duration-of-a-mediaplayer – Webrsk 2011-04-07 10:49:37