获取 音频的 大小 和 时长


pom.xml 依赖下载 两个都可

<!--获取文件时长的jar-->
第一个:
<dependency>
<groupId>com.github.dadiyang</groupId>
<artifactId>jave</artifactId>
<version>1.0.5</version>
</dependency>
------------------------------------------
或者 第二个:
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.3</version>
</dependency>
--------------------------------------------------------------------------------------------

long length = file.length();
getFormatSize(length);

try {
MP3File f = (MP3File) AudioFileIO.read(file);
MP3AudioHeader audioHeader = (MP3AudioHeader)f.getAudioHeader();
System.out.println(audioHeader.getTrackLength());
} catch(Exception e) {
e.printStackTrace();
}


public static String getFormatSize(double size) {
double kiloByte = size/1024;
if(kiloByte < 1) {
return size + "Byte(s)";
}

double megaByte = kiloByte/1024;
if(megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
}

double gigaByte = megaByte/1024;
if(gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
}

double teraBytes = gigaByte/1024;
if(teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
}

posted @ 2019-08-16 17:32  我是一名搬运工  阅读(951)  评论(0编辑  收藏  举报