Java輕松使用工具類實現(xiàn)獲取MP3音頻時長
更新時間:2021年10月27日 15:10:55 作者:劍客阿良_ALiang
在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用工具類來獲取一個MP3音頻文件的時間長度,感興趣的同學(xué)繼續(xù)往下閱讀吧
獲取mp3格式音頻時長。
Maven依賴
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.1</version>
</dependency>
代碼
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import java.io.File;
/** @Author huyi @Date 2021/9/30 15:06 @Description: mp3音頻工具 */
public class AudioMp3Utils {
/**
* 獲取mp3語音文件播放時長(秒) mp3
*
* @param filePath
* @return
*/
public static void getMp3Duration(String filePath) {
try {
File mp3File = new File(filePath);
MP3File f = (MP3File) AudioFileIO.read(mp3File);
MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
System.out.println("時長:" + Float.parseFloat(audioHeader.getTrackLength() + ""));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getMp3Duration("E:\\csdn\\dzgz.mp3");
}
}
執(zhí)行結(jié)果:

說明
單位為秒,酌情使用。

到此這篇關(guān)于Java輕松使用工具類實現(xiàn)獲取MP3音頻時長的文章就介紹到這了,更多相關(guān)Java 獲取MP3音頻時長內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA Stack詳細(xì)介紹和示例學(xué)習(xí)
JAVA Stack是棧。它的特性是:先進(jìn)后出(FILO, First In Last Out)。2013-11-11
spring cloud gateway 限流的實現(xiàn)與原理
這篇文章主要介紹了spring cloud gateway 限流的實現(xiàn)與原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
SpringBoot通過@Value實現(xiàn)給靜態(tài)變量注入值詳解
這篇文章主要介紹了springboot如何通過@Value給靜態(tài)變量注入值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

