java解析mp3工具
程序员文章站
2022-06-21 15:08:44
...
需要的jar包为jid3lib.jar,sf上的http://javamusictag.sourceforge.net/download.htm不支持中文,附件源码中的为修改版。
MP3Util.java
package com.gary.util;
import java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.AbstractID3v2;
import org.farng.mp3.id3.ID3v1;
import org.farng.mp3.lyrics3.AbstractLyrics3;
/**
* MP3工具
*
* supports:
* ID3 v1 & v1.1
* Lyrics3 v1 & v2
* ID3 v2.2 & v2.3 & v2.4 tags
* MP3 Frame Headers
*
* @author gary
*
*/
public class MP3Util {
public static MP3Info getMP3Info(String path) throws IOException, TagException{
MP3File file = new MP3File(path);
AbstractID3v2 id3v2 = file.getID3v2Tag();
ID3v1 id3v1 = file.getID3v1Tag();
AbstractLyrics3 lyrics = file.getLyrics3Tag();
if(id3v2 != null){
return new MP3Info(id3v2.getLeadArtist(), id3v2.getSongTitle(),
id3v2.getAlbumTitle(), id3v2.getTrackNumberOnAlbum(),
id3v2.getYearReleased(), lyrics == null ? null : lyrics.getSongLyric());
}else if(id3v1 != null){
return new MP3Info(id3v1.getLeadArtist(), id3v1.getSongTitle(),
id3v1.getAlbumTitle(), id3v1.getTrackNumberOnAlbum(),
id3v1.getYearReleased(), lyrics == null ? null : lyrics.getSongLyric());
}else{
return null;
}
}
}
MP3Info.java
package com.gary.util;
/**
* MP3信息
* @author gary
*
*/
public class MP3Info {
private String artist;
private String songTitle;
private String albumTitle;
private String trackNumberOnAlbum;
private String yearReleased;
private String songLyric;
public MP3Info(String artist, String songTitle, String albumTitle,
String trackNumberOnAlbum, String yearReleased, String songLyric) {
this.artist = artist;
this.songTitle = songTitle;
this.albumTitle = albumTitle;
this.trackNumberOnAlbum = trackNumberOnAlbum;
this.yearReleased = yearReleased;
this.songLyric = songLyric;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getSongTitle() {
return songTitle;
}
public void setSongTitle(String songTitle) {
this.songTitle = songTitle;
}
public String getAlbumTitle() {
return albumTitle;
}
public void setAlbumTitle(String albumTitle) {
this.albumTitle = albumTitle;
}
public String getTrackNumberOnAlbum() {
return trackNumberOnAlbum;
}
public void setTrackNumberOnAlbum(String trackNumberOnAlbum) {
this.trackNumberOnAlbum = trackNumberOnAlbum;
}
public String getYearReleased() {
return yearReleased;
}
public void setYearReleased(String yearReleased) {
this.yearReleased = yearReleased;
}
public String getSongLyric() {
return songLyric;
}
public void setSongLyric(String songLyric) {
this.songLyric = songLyric;
}
public void printMP3Info(){
System.out.println("歌手:" + artist);
System.out.println("歌曲名:" + songTitle);
System.out.println("专辑名:" + albumTitle);
System.out.println("音轨:" + trackNumberOnAlbum);
System.out.println("发行年份:" + yearReleased);
System.out.println("歌词:" + songLyric);
}
}
test:@Test
public void testGetMP3Info() {
try {
MP3Util.getMP3Info("song.mp3").printMP3Info();
} catch (IOException e) {
System.out.println("文件未找到");
} catch (TagException e) {
System.out.println("读取标签错误");
e.printStackTrace();
}
}
上一篇: python 利用mutagen读取MP3 ID3信息
下一篇: 分页查询