WAV音頻文件獲取時長工具類


package com.weizui.demo.util;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
import java.net.URL;

/**
 * Function:
 *
 * @author zhengyou
 * Created on 2018/2/7 11:55
 * @since JDK 1.7
 **/

public class AudioUtil {

    /**
     * 獲取音頻文件時長
     *
     * @param wavFilePath wav文件路徑,支持本地和網絡HTTP路徑
     * @return 時長/微秒,可 /1000000D 得到秒
     * @throws Exception
     */
    public static long getMicrosecondLengthForWav(String wavFilePath) throws Exception {

        if (wavFilePath == null || wavFilePath.length() == 0) {
            return 0;
        }
        String bath = wavFilePath.split(":")[0];
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais;
        if ("http".equals(bath.toLowerCase())||"https".equals(bath.toLowerCase())) {
            ais = AudioSystem.getAudioInputStream(new URL(wavFilePath));
        } else {
            ais = AudioSystem.getAudioInputStream(new File(wavFilePath));
        }
        clip.open(ais);
        return clip.getMicrosecondLength();
    }


    public static void main(String[] args) throws Exception {
        String wavUrl = "http://data.huiyi8.com/2017/gha/08/19/1969.wav";
        long microsecondLengthForWav = getMicrosecondLengthForWav(wavUrl);
        System.out.println(microsecondLengthForWav);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM