Java輕松使用工具類實現(xiàn)獲取wav時間長度
獲取wav格式音頻時長。
Maven依賴
<dependency> <groupId>org</groupId> <artifactId>jaudiotagger</artifactId> <version>2.0.1</version> </dependency>
工具類
import org.jaudiotagger.audio.wav.util.WavInfoReader; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; /** @Author huyi @Date 2021/9/30 14:46 @Description: 音頻工具類 */ public class AudioWavUtils { public static void getWavInfo(String filePath) throws Exception { File file = new File(filePath); WavInfoReader wavInfoReader = new WavInfoReader(); RandomAccessFile raf = new RandomAccessFile(file, "r"); // wav音頻時長 long duration = (long) (wavInfoReader.read(raf).getPreciseLength() * 1000); // wav音頻采樣率 int sampleRate = toInt(read(raf, 24, 4)); System.out.println("duration -> " + duration + ",sampleRate -> " + sampleRate); raf.close(); } public static int toInt(byte[] b) { return ((b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0])); } public static byte[] read(RandomAccessFile rdf, int pos, int length) throws IOException { rdf.seek(pos); byte[] result = new byte[length]; for (int i = 0; i < length; i++) { result[i] = rdf.readByte(); } return result; } public static void main(String[] args) throws Exception { getWavInfo("E:\\csdn\\dzgz.wav"); } }
輸出結(jié)果:
duration為音頻時長,單位毫秒,sampleRate為采樣率。
說明
該工具類只能處理單聲道音頻,雙聲道會報錯,多聲道屬于立體聲范疇,提醒一下。
到此這篇關(guān)于Java輕松使用工具類實現(xiàn)獲取wav時間長度的文章就介紹到這了,更多相關(guān)Java 獲取wav時長內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis異常-Property ''configLocation'' not specified, using d
今天小編就為大家分享一篇關(guān)于MyBatis異常-Property 'configLocation' not specified, using default MyBatis Configuration,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Java異常簡介和架構(gòu)_動力節(jié)點Java學(xué)院整理
這篇文章主要分享了Java異常簡介和架構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06