package com.fuyuanshen.equipment.utils;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3File;
import java.io.File;

public class Mp3Duration {

    /**
     * 获取MP3文件的播放时长（秒）
     *
     * @param filePath MP3文件路径
     * @return 播放时长（秒）
     */
    public static int getMp3Duration(String filePath) {
        try {
            File file = new File(filePath);
            MP3File mp3File = (MP3File) AudioFileIO.read(file);
            return mp3File.getMP3AudioHeader().getTrackLength();
        } catch (Exception e) {
            e.printStackTrace();
            return -1; // 错误时返回-1
        }
    }

    public static void main(String[] args) {
        String filePath = "D:\\http_output.mp3"; // 替换为实际路径
        int duration = getMp3Duration(filePath);
        if (duration != -1) {
            System.out.println("MP3时长: " + duration + " 秒");
        } else {
            System.out.println("无法获取MP3时长");
        }
    }
}
