mp3 标题信息阅读器不工作

发布于 2024-11-08 01:51:47 字数 2150 浏览 0 评论 0原文

大家好 我从以下代码中获取比特率。我有一首比特率为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2024-11-15 01:51:47

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

you need to call mp3file.seekMP3Frame(); before attempting to retrieve bitrate, this method will read the file headers including the bitrate.

许你一世情深 2024-11-15 01:51:47

有点偏离主题,但尝试 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文