mp3 标题信息阅读器不工作
大家好 我从以下代码中获取比特率。我有一首比特率为 128kbps 的歌曲。但它只打印 0。请帮我。 代码: 导入java.io.File; 导入java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.AbstractID3v1;
import org.farng.mp3.id3.AbstractID3v2;
public class ID3Reader {
File sourcefile;
MP3File mp3file;
public ID3Reader(String filename) {
sourcefile = new File(filename);
try {
mp3file = new MP3File(sourcefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TagException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main( String[] args )
{
ID3Reader id3reader = new ID3Reader("D:\\Mp3\\taxi.mp3");
id3reader.displayinformations();
}
private void displayinformations() {
String title="", artist="", album="", genre="", year="";
System.out.println("bit rate"+mp3file.getBitRate()); // print bit rate as 0(zero).
if(mp3file.hasID3v1Tag()){
AbstractID3v1 tag = mp3file.getID3v1Tag();
if(tag != null){
try{ title = tag.getSongTitle(); }catch(Exception e){}
try{ artist = tag.getLeadArtist(); }catch(Exception e){}
try{ album = tag.getAlbumTitle(); }catch(Exception e){}
try{ genre = tag.getSongGenre(); }catch(Exception e){}
try{ year = tag.getYearReleased(); }catch(Exception e){}
}
}else if(mp3file.hasID3v2Tag()){
AbstractID3v2 tag = mp3file.getID3v2Tag();
if(tag != null){
title = tag.getFrame("TT2").toString();
artist = tag.getFrame("TP1").toString();
album = tag.getFrame("TAL").toString();
year = tag.getFrame("TYE").toString();
genre = tag.getFrame("TCO").toString();
}
}
System.out.println("Titre : " + title);
System.out.println("Artiste : " + artist);
System.out.println("Album : " + album);
System.out.println("Genre : " + genre);
System.out.println("Annee : " + year);
}
}
hi all
i get bit rate from the following code . i have a song with bit rate is 128kbps. but it print 0 only. please help me.
code:
import java.io.File;
import java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.AbstractID3v1;
import org.farng.mp3.id3.AbstractID3v2;
public class ID3Reader {
File sourcefile;
MP3File mp3file;
public ID3Reader(String filename) {
sourcefile = new File(filename);
try {
mp3file = new MP3File(sourcefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TagException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main( String[] args )
{
ID3Reader id3reader = new ID3Reader("D:\\Mp3\\taxi.mp3");
id3reader.displayinformations();
}
private void displayinformations() {
String title="", artist="", album="", genre="", year="";
System.out.println("bit rate"+mp3file.getBitRate()); // print bit rate as 0(zero).
if(mp3file.hasID3v1Tag()){
AbstractID3v1 tag = mp3file.getID3v1Tag();
if(tag != null){
try{ title = tag.getSongTitle(); }catch(Exception e){}
try{ artist = tag.getLeadArtist(); }catch(Exception e){}
try{ album = tag.getAlbumTitle(); }catch(Exception e){}
try{ genre = tag.getSongGenre(); }catch(Exception e){}
try{ year = tag.getYearReleased(); }catch(Exception e){}
}
}else if(mp3file.hasID3v2Tag()){
AbstractID3v2 tag = mp3file.getID3v2Tag();
if(tag != null){
title = tag.getFrame("TT2").toString();
artist = tag.getFrame("TP1").toString();
album = tag.getFrame("TAL").toString();
year = tag.getFrame("TYE").toString();
genre = tag.getFrame("TCO").toString();
}
}
System.out.println("Titre : " + title);
System.out.println("Artiste : " + artist);
System.out.println("Album : " + album);
System.out.println("Genre : " + genre);
System.out.println("Annee : " + year);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试检索比特率之前,您需要调用 mp3file.seekMP3Frame();,此方法将读取包括比特率的文件头。
you need to call
mp3file.seekMP3Frame();
before attempting to retrieve bitrate, this method will read the file headers including the bitrate.有点偏离主题,但尝试 JAudioTagger 看看是否会带来更好的结果。我尝试过一些读取/写入 MP3 标签的库,而 JAudioTagger 是我最满意的一个。
http://www.jthink.net/jaudiotagger/examples_id3.jsp
A little off topic, but try JAudioTagger and see if that gives you better results. I've tried a few libraries which reads/writes MP3 tags, and JAudioTagger is the one I've been most happy with.
http://www.jthink.net/jaudiotagger/examples_id3.jsp