我是个新手,因为要做一个mp3的播放软件,网上搜到了一个读取信息的代码,在这里想分享下。原作者是 NullPointException
首先需要下载 jaudiotagger-2.2.2.jar 包,里面封装了获取信息的一些方法。网址:http://www.jthink.net/jaudiotagger/
下载好之后导入到工程当中去,之后就可以开始了。
package mp3Info;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.id3.AbstractID3Tag;
import org.jaudiotagger.tag.id3.AbstractID3v1Tag;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.ID3v1Tag;
import org.jaudiotagger.tag.id3.ID3v24Tag;
import org.jaudiotagger.tag.images.Artwork;
public class JauDiotaggerTest {
public static void main(String[] args) {
getHead();
getContent();
}
private static String mp3Path = "D:\\Music\\QQMusic\\大野雄二 - A ROSE TATOO.mp3";
private static MP3File mp3File;
private static final int START=6;
private static void getHead() {
try {
System.out.println("----------------Loading...Head-----------------");
mp3File = new MP3File(mp3Path);//封装好的类
MP3AudioHeader header = mp3File.getMP3AudioHeader();
System.out.println("时长: " + header.getTrackLength()); //获得时长
System.out.println("比特率: " + header.getBitRate()); //获得比特率
System.out.println("音轨长度: " + header.getTrackLength()); //音轨长度
System.out.println("格式: " + header.getFormat()); //格式,例 MPEG-1
System.out.println("声道: " + header.getChannels()); //声道
System.out.println("采样率: " + header.getSampleRate()); //采样率
System.out.println("MPEG: " + header.getMpegLayer()); //MPEG
System.out.println("MP3起始字节: " + header.getMp3StartByte()); //MP3起始字节
System.out.println("精确的音轨长度: " + header.getPreciseTrackLength()); //精确的音轨长度
} catch (Exception e) {
System.out.println("没有获取到任何信息");
}
}
private static void getContent() {
try {
System.out.println("----------------Loading...Content-----------------");
AbstractID3v2Tag id3v2tag= mp3File.getID3v2Tag();
String songName=new String(id3v2tag.frameMap.get("TIT2").toString().getBytes("ISO-8859-1"),"GB2312");
String singer=new String(id3v2tag.frameMap.get("TPE1").toString().getBytes("ISO-8859-1"),"GB2312");
String author=new String(id3v2tag.frameMap.get("TALB").toString().getBytes("ISO-8859-1"),"GB2312");
System.out.println("歌名:"+songName.substring(START, songName.length()-3));
System.out.println("歌手:"+singer.substring(START,singer.length()-3));
System.out.println("专辑名:"+author.substring(START,author.length()-3));
} catch (Exception e) {
System.out.println("没有获取到任何信息");
}
System.out.println("All Info:"+mp3File.displayStructureAsPlainText());
}
}
输出结果:
----------------Loading...Head-----------------
时长: 202
比特率: 128
音轨长度: 202
格式: MPEG-1 Layer 3
声道: Joint Stereo
采样率: 44100
MPEG: Layer 3
MP3起始字节: 9216
精确的音轨长度: 202.84081632653061
----------------Loading...Content-----------------
歌名:A ROSE TATOO
歌手:大野雄二
专辑名:Drive Groovin' Lupin - Lupin the Third
All Info:file:D:\Music\QQMusic\大野雄二 - A ROSE TATOO.mp3
tag:ID3v1.1.0
title:A ROSE TATOO
artist:?ó?°????
album:Drive Groovin' Lupin - Lupin t
year:2005
comment:
track:7
genre:12
tag:ID3v2.30
duplicateBytes:0
duplicateFrameId:
emptyFrameBytes:0
fileReadSize:9206
invalidFrames:0
header:
unsyncronisation:false
extended:false
experimental:false
crcdata:0
paddingsize:0
body:
frame:APIC
frameSize:4899
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
MIMEType:image/jpeg
PictureType:3
Description:
PictureData:[B@1201a25
frame:TIT2
frameSize:13
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:A ROSE TATOO
frame:TRCK
frameSize:2
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:7
frame:TYER
frameSize:5
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:2005
frame:TCON
frameSize:14
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:JAZZ & FUSION
frame:TALB
frameSize:39
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:Drive Groovin' Lupin - Lupin the Third
frame:TPE2
frameSize:9
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:?ó?°????
frame:TPOS
frameSize:4
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:1/1
frame:PRIV
frameSize:33
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
Owner:ZuneCollectionID
Data:[B@94948a
frame:TPE1
frameSize:9
statusFlags:
typeTagAlterPreservation:0
typeFileAlterPreservation:0
typeReadOnly:0
encodingFlags:
compression:0
encryption:0
groupidentity:0
body:
TextEncoding:0
Text:?ó?°????
注:关于转码还有专辑图片等方面还是不是很清楚,希望日后能补上。最后,感谢原作者的分享。