/**
* 获取读音时长 单位:s
* @param mp3Url
* @return
* @throws IOException
* @throws Exception
*/
public static int ReadMp3(String mp3Url) throws IOException, Exception {
URL url = new URL(mp3Url);
URLConnection con = null;
try {
con = url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
Bitstream bt = new Bitstream(bis);
//获取mp3时间长度
Header header = bt.readFrame();
int mp3Length = con.getContentLength();
int time = (int) header.total_ms(mp3Length);
return time / 1000;
}
当url存在特殊符号或空格时con.getInputStream()会出现java.io.IOException: Server returned HTTP response code: 400 for URL,需要用URLEncoder.encode(url, "UTF-8")进行转义,默认转义空格为“+”,需要用url = url.replaceAll("+", "%20")替换为%20
<dependency>
<groupId>com.badlogicgames.jlayer</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.2-gdx</version>
</dependency>