2011-03-24 31 views
1

使用Mediaplayer播放音頻文件。Android Mediaplayer - 基於seekbar進度更新文本視圖中音頻的已用時間

一切工作正常,但音頻流逝的時間需要在textview中更新。

textview.setText(mediaplayer.getCurrentPosition()/100000.0).toString(); 

這給雙重價值,但如果結束時間超過一分鐘,並經過時間更是1分鐘它顯示0:60 ...但它應該1:00 ...

有沒有其他方法來顯示基於seekbar進度的經過時間?

謝謝。

+0

得到了另一篇文章的答案。看看這個鏈接的答案。 http://stackoverflow.com/questions/5548922/how-do-i-correctly-display-the-position-duration-of-a-mediaplayer – Webrsk 2011-04-07 10:49:37

回答

7

該工程─回答另一個後採取什麼樣的雲: How do I correctly display the position/duration of a MediaPlayer?

日期格式適用於日期,而不是時間間隔。因此,如果您獲得1秒的位置,則數據格式將其解釋爲表示日曆開始之後的日期/時間爲1秒。


private String getTimeString(long millis) { 
    StringBuffer buf = new StringBuffer(); 

    int hours = millis/(1000*60*60); 
    int minutes = (millis % (1000*60*60))/(1000*60); 
    int seconds = ((millis % (1000*60*60)) % (1000*60))/1000; 

    buf 
     .append(String.format("%02d", hours)) 
     .append(":") 
     .append(String.format("%02d", minutes)) 
     .append(":") 
     .append(tring.format("%02d", seconds)); 

    return buf.toString(); 
} 

然後像做


totalTime.setText(getTimeString(duration)); 
currentTime.setText(getTimeString(position)); 
0

您可以將getCurrentPosition轉換爲日期並使用dateFormat設置您的輸出字符串。

Date d = new Date(); 
d.setTime(mediaplayer.getCurrentPosition()); 
private final SimpleDateFormat timeFormat = new SimpleDateFormat("HH.mm"); 
String time = timeFormat.format(d).toString(); 
+0

感謝您的迴應。但它總是顯示05:30。沒有工作! – Webrsk 2011-03-25 05:40:18

1

音樂樣本應用程序有一個類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> 
+0

感謝nheid的回答。但是在R.string.durationformatshort中需要什麼文本:R.string.durationformatlong? – Webrsk 2011-04-01 10:57:57

+0

我試過carlovv解決方案,但它只顯示05:30,我試圖將getCurrentPosition()除以1000,但它仍然沒有工作! – Webrsk 2011-04-01 10:58:59

相關問題