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

故事扮演 提交于 2019-12-13 01:24:29

问题


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.


回答1:


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



回答2:


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>



回答3:


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


来源:https://stackoverflow.com/questions/5418644/android-mediaplayer-based-on-seekbar-progress-update-the-elapsed-time-of-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!