Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中以分钟和秒为单位获取音频长度_Java_Android_C_Android Layout_Github - Fatal编程技术网

Java 如何在android中以分钟和秒为单位获取音频长度

Java 如何在android中以分钟和秒为单位获取音频长度,java,android,c,android-layout,github,Java,Android,C,Android Layout,Github,发件人: 如果没有可用的持续时间,则以毫秒为单位的持续时间(对于 例如,如果流式传输实时内容),则返回-1 这就是为什么您将从getDuration()获取以毫秒为单位的持续时间。 您可以使用此字符串获取MediaPlayer的时间: public static String milliSecondsToTimer(long milliseconds) { String finalTimerString = ""; String secondsString = "

发件人:

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

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

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;
    }
然后,当你在问题中写道:

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

3岁了,但没人回答正确,那我就回答

TextView textView = (TextView) findViewById(R.id.tvduration); 
textView.setText(time);
如果allSeconds大于60,我们将分分钟和秒,然后使用以下方法将allSeconds=68转换为分钟:

allSeconds = 68950 / 1000 = 68 seconds
剩下的数字将是秒数

minutes = allSeconds / 60 = 1 left 8
无论秒数是否小于10,formatSeconds(字节秒)方法都会添加一个零

因此,最终结果将是:
1:08


为什么是字节?Long的性能不好,因此最好使用字节进行更长的操作。

使用此方法时,持续时间以毫秒为单位显示,但我想显示音频的持续时间,如15分钟:32秒请先进行一些研究。您不知道如何将毫秒转换为秒或分钟吗?我的问题在前面的消息中已解决。谢谢你的回答。你应该再次将“持续时间”设置为textView吗?
allSeconds = 68950 / 1000 = 68 seconds
minutes = allSeconds / 60 = 1 left 8
seconds = allSeconds % 60 = 8