操作MP3格式的一些java方法

一:java获取MP3文件信息

(歌手,歌名,封面,专辑,时长)

例如: 

 + 依赖

  <dependency>
            <groupId>org</groupId>
            <artifactId>jaudiotagger</artifactId>
            <version>2.0.3</version>
        </dependency>

+code

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Mp3Util {

    public static void main(String[] args) {
        try {
            getMP3Info();
        } catch (ReadOnlyFileException e) {
            e.printStackTrace();
        } catch (TagException e) {
            e.printStackTrace();
        } catch (InvalidAudioFrameException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CannotReadException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取MP3歌曲名、歌手、时长、照片信息
     * @param url
     * @return
     */
    public static void getMP3Info() throws ReadOnlyFileException, TagException, InvalidAudioFrameException, IOException, CannotReadException {
      String  url = "D:\\Download\\告白气球 - 周杰伦.mp3";//测试数据**

        MP3File mp3File = (MP3File) AudioFileIO.read(new File(url));
        AbstractID3v2Tag v2tag = mp3File.getID3v2Tag();

        String artist = v2tag.getFirst(FieldKey.ARTIST);// 歌手名
        String album = v2tag.getFirst(FieldKey.ALBUM);// 專輯名
        String songName = v2tag.getFirst(FieldKey.TITLE);// 歌名
        System.out.println("album: " + album); // 專輯名
        System.out.println("singer: " + artist); // 歌手名
        System.out.println("songName: " + songName); // 歌名

        MP3AudioHeader header = mp3File.getMP3AudioHeader(); // mp3文件頭部信息
        int length = header.getTrackLength();
        System.out.println("Length: " + length / 60 + ":" + length % 60 + "sec"); // 歌曲時長
        AbstractID3v2Tag tag = mp3File.getID3v2Tag();
        AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC");

        FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody();
        byte[] imageData = body.getImageData();
        //System.out.println(imageData);
        Image img=Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length);
        System.out.println("img----" + imageData);
        ImageIcon icon = new ImageIcon(img);
        FileOutputStream fos = new FileOutputStream("D://test1.jpg");
        fos.write(imageData);
        fos.close();
        getImg(icon);
    }

    public static void getImg(ImageIcon img){
        JFrame f = new JFrame();
        JLabel l = new JLabel();
        l.setIcon(img);
        l.setVisible(true);
        f.add(l);
        f.setSize(500, 500);
        f.setVisible(true);
    }
}


+print

 -over 仅供交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arenaschi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值