java获取mp3的时长和播放mp3文件

所需包为jaudiotagger-2.2.6-SNAPSHOT.jar和jl1.0.1.jar。

复制代码
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;

import javazoom.jl.player.Player;

public class MusicUtil {

    public static void play(String position) {
        try {
            BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(position));
            Player player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static int getDuration(String position) {

        int length = 0;
        try {
            MP3File mp3File = (MP3File) AudioFileIO.read(new File(position));
            MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();

            // 单位为秒
            length = audioHeader.getTrackLength();

            return length;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }
    
    public static void main(String[] args) {
        String position = "逃跑计划-夜空中最亮的星.mp3";
        getDuration(position);
        play(position);
    }
}
复制代码

 

posted @   吉良吉影的冒险  阅读(5792)  评论(2)    收藏  举报
编辑推荐:
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
阅读排行:
· 我用这13个工具,让开发效率提升了5倍!
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· 32岁入行STM32迟吗?
· Coze工作流实战:一键生成鸡汤视频——厉害的人,早已戒掉情绪
· C#.Net筑基-泛型T & 协变逆变
点击右上角即可分享
微信分享提示