欢迎光临
一个废柴的博客

Java代码获取Mp3音乐的歌名、歌手、专辑名等信息

这里介绍的是使用Jaudiotagger库获取Mp3音频文件的歌名、歌手、专辑名等信息。使用的jaudiotagger-2.2.6-SNAPSHOT.jar包下载地址:Download

实现代码如下:

package music;

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.AbstractID3v2Tag;

import java.io.File;
import java.io.IOException;

/**
 * Created by https://www.rxblog.xyz on 2017-08-10.
 */
public class GetMp3Info {
    public static void main(String[] args) throws CannotReadException, IOException, TagException,
            ReadOnlyFileException, InvalidAudioFrameException {
        MP3File mp3File = (MP3File) AudioFileIO.read(new File("D:\\CloudMusic\\张国荣 - 倩女幽魂.mp3"));
        AbstractID3v2Tag v2tag = mp3File.getID3v2Tag();
        //String artist = v2tag.getFirst(ID3v24Frames.FRAME_ID_ARTIST);
        //String album = v2tag.getFirst(ID3v24Frames.FRAME_ID_ALBUM);
        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"); // 歌曲时长
    }
}

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址