简体   繁体   English

Android Mediaplayer-基于seekbar进度更新textview中音频的经过时间

[英]Android Mediaplayer - based on seekbar progress update the elapsed time of audio in textview

Using Mediaplayer to play the audio file. 使用Mediaplayer播放音频文件。

Everything works fine but the elapsed time of audio need to be updated in textview. 一切正常,但是音频的播放时间需要在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... 这给出了双精度值,但是如果结束时间超过一分钟,并且经过的时间为1分钟,则显示0:60 ...但应为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? 这有效-取自另一篇文章的答案: 如何正确显示MediaPlayer的位置/持续时间?

DateFormat works for dates, not for time intervals. DateFormat适用于日期,不适用于时间间隔。 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. 因此,如果位置为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();
}

And then do something like 然后做类似的事情


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

The Music sample app has a class MusicUtils : 音乐示例应用程序具有一个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();
}

which you can use. 您可以使用。

But carlovv's solution will work too, if you divide the result of getCurrentPosition() by 1000, as the method returns milliseconds. 但是,如果将getCurrentPosition()的结果除以1000,则carlovv的解决方案也将起作用,因为该方法将返回毫秒。

this is what goes in your strings.xml 这就是你的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>

you can convert getCurrentPosition into date and use a dateFormat to set your output string. 您可以将getCurrentPosition转换为date并使用dateFormat设置输出字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM