【问题标题】:how to get length of audio in minutes and seconds in android如何在android中以分钟和秒为单位获取音频长度
【发布时间】:2017-10-05 08:16:40
【问题描述】:
int duration = mediaPlayer.getDuration(); 
textView = (TextView) findViewById(R.id.tvduration); textView.setText(duration);

【问题讨论】:

  • 持续时间通过这种方法以毫秒为单位显示,但我想显示音频的持续时间,如 15 分钟:32 秒
  • 请先做一些研究你不知道如何将毫秒转换为秒或分钟吗?

标签: java android c android-layout github


【解决方案1】:

来自MediaPlayer

持续时间以毫秒为单位,如果没有可用的持续时间(对于 例如,如果流式传输实时内容),则返回 -1。

这就是为什么您将从getDuration() 获得持续时间(以毫秒为单位)。
您可以使用它来获取MediaPlayer 的时间作为字符串:

int duration = mediaPlayer.getDuration();
String time = String.format("%02d min, %02d sec", 
    TimeUnit.MILLISECONDS.toMinutes(duration),
    TimeUnit.MILLISECONDS.toSeconds(duration) - 
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);

然后正如你在问题中所写:

TextView textView = (TextView) findViewById(R.id.tvduration); 
textView.setText(time);

【讨论】:

  • Y 是否应该再次将“duration”设置为 textView?
【解决方案2】:
public static String milliSecondsToTimer(long milliseconds) {
        String finalTimerString = "";
        String secondsString = "";

        //Convert total duration into time
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours == 0) {
            finalTimerString = hours + ":";
        }

        // Pre appending 0 to seconds if it is one digit
        if (seconds == 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }

        finalTimerString = finalTimerString + minutes + ":" + secondsString;

        // return timer string
        return finalTimerString;
    }

【讨论】:

  • 我的问题已在上一条消息中解决。非常感谢您的回答。
【解决方案3】:

已经 3 岁了,但没有人正确回复,我会回复

public String format(long millis) {
    long allSeconds = millis / 1000;
    int allMinutes;
    byte seconds, minutes, hours;
    if (allSeconds >= 60) {
        allMinutes = (int) (allSeconds / 60);
        seconds = (byte) (allSeconds % 60);
        if (allMinutes >= 60) {
            hours = (byte) (allMinutes / 60);
            minutes = (byte) (allMinutes % 60);
            return String.format(Locale.US, "%d:%d:" + formatSeconds(seconds), hours, minutes, seconds);
        } else
            return String.format(Locale.US, "%d:" + formatSeconds(seconds), allMinutes, seconds);
    } else
        return String.format(Locale.US, "0:" + formatSeconds((byte) allSeconds), allSeconds);
}

public String formatSeconds(byte seconds) {
    String secondsFormatted;
    if (seconds < 10) secondsFormatted = "0%d";
    else secondsFormatted = "%d";
    return secondsFormatted;
}

millis / 1000 将毫秒转换为秒。示例:

allSeconds = 68950 / 1000 = 68 seconds

如果 allSeconds 大于 60,我们会将分钟与秒分开,然后我们将 allSeconds = 68 转换为分钟,使用:

minutes = allSeconds / 60 = 1 left 8

剩下的数字是秒

seconds = allSeconds % 60 = 8

formatSeconds(byte seconds) 方法在秒数小于 10 时加零。

所以最后会是:1:08

为什么是字节? Long 的性能很差,那么最好使用字节来进行更长的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多