开发者

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

开发者 https://www.devze.com 2023-02-19 16:42 出处:网络
Using Mediaplayer to play the audio file. Everything works fine but the elapsed time of audio need to be updated in textview.

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.getCurrentPos开发者_如何转开发ition()/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));


The Music sample app has a class 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.

this is what goes in your 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.

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

精彩评论

暂无评论...
验证码 换一张
取 消