android 音频amr文件时长

一、文件时长获取

1
2
3
4
5
6
7
8
9
10
11
public int getDurations(){
String curAudioFile = “XXX.amr”;

MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(curAudioFile);

mediaPlayer.prepare();

return mediaPlayer.getDuration();// 单位毫秒
}

二、文件时长转换**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
private static String getAudioDuration(int nDuration0) {

DecimalFormat df = new DecimalFormat("#.00");

String fileSizeString = "";

String wrongSize = "0ms";

if (nDuration0 == 0) {

return wrongSize;

}

if (nDuration0 < 1000) {

fileSizeString = df.format((double) nDuration0) + "ms";

} else if (nDuration0 < 60000) {

fileSizeString = df.format((double) nDuration0 / 1000) + "s";

} else if (nDuration0 < 3600000) {

fileSizeString = df.format((double) nDuration0 / 60000) + "min";

} else {

fileSizeString = df.format((double) nDuration0 / 3600000) + "h";

}

return fileSizeString;

}
坚持原创技术分享,您的支持将鼓励我继续创作!