本文介绍了Android Mediaplayer - 基于seekbar进度更新textview中音频的经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Mediaplayer 播放音频文件.

一切正常,但需要在 textview 中更新音频的经过时间.

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

这给出了 double 值,但如果结束时间超过一分钟并且经过的时间为 1 分钟,则显示 0:60...但它应该为 1:00...

有没有其他方法可以根据搜索栏进度显示经过的时间?

谢谢.

解决方案

This works- 答案取自另一篇文章:如何正确显示媒体播放器?

DateFormat 适用于日期,不适用于时间间隔.因此,如果您获得 1 秒的位置,则数据格式会将其解释为日期/时间是日历开始后的 1 秒.

私人字符串 getTimeString(长毫秒){StringBuffer buf = new StringBuffer();int 小时 = 毫秒/(1000*60*60);int 分钟 = ( 毫秒 % (1000*60*60) )/(1000*60);int秒=((毫秒%(1000 * 60 * 60))%(1000 * 60))/1000;缓冲区.append(String.format("%02d", 小时)).附加(":").append(String.format("%02d", 分钟)).附加(":").append(tring.format("%02d", seconds));返回 buf.toString();}

然后做类似的事情

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

Using Mediaplayer to play the audio file.

Everything works fine but the elapsed time of audio need to be updated in textview.

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

This gives the double value but if the end time is more than a minute and the elapsed time is 1minute it shows 0:60... but it should as 1:00...

Is there anyother way to show the elapsed time based on seekbar progress?

Thanks.

解决方案

This works- Answer taken from another post :How do I correctly display the position/duration of a MediaPlayer?

DateFormat works for dates, not for time intervals. So if you get a position of 1 second, the data format interprets this as meaning that the date/time is 1 second after the beginning the calendar.


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();
}

And then do something like


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

这篇关于Android Mediaplayer - 基于seekbar进度更新textview中音频的经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:19