MP3标题信息阅读器不工作

时间:2011-05-19 07:38:53

标签: java javasound

大家好 我从以下代码获得比特率。我有一首比特率为128kbps的歌曲。但它只打印0。请帮我。 码:       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);
}       
}

2 个答案:

答案 0 :(得分:1)

在尝试检索比特率之前,您需要调用mp3file.seekMP3Frame();,此方法将读取包含比特率的文件头。

答案 1 :(得分:0)

有点偏离主题,但尝试JAudioTagger,看看是否能给你带来更好的效果。我尝试了一些读/写MP3标签的库,而JAudioTagger是我最满意的。

http://www.jthink.net/jaudiotagger/examples_id3.jsp