【问题标题】:Fetching MP3-Tags while recording a stream using java使用java录制流时获取MP3-Tags
【发布时间】:2018-02-18 18:06:09
【问题描述】:

我目前有这个代码,它成功记录了一个(mp3-)流:

public class Recorder {
    private static final int BUFFER_SIZE = 2048;

    private Thread thread;
    private boolean running = false;

    public Recorder(URL stream, File dest) {
        thread = new Thread(() -> {
            try {
                URLConnection connection = stream.openConnection();
                InputStream inStream = connection.getInputStream();

                OutputStream outStream = new FileOutputStream(dest);
                byte[] buffer = new byte[BUFFER_SIZE];
                int length;

                System.out.println("Now recording " + stream.toString());

                while ((length = inStream.read(buffer)) > 0 && running) {
                    outStream.write(buffer, 0, length);
                }

                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public void start() {
        running = true;
        thread.start();
    }

    public void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

如何正确读取流中的 MP3 标签(歌曲名称和艺术家姓名)?我找到了一些关于如何从文件中获取 MP3 标记的答案,但不是从流音频中获取。

谢谢。

【问题讨论】:

    标签: java audio io streaming mp3


    【解决方案1】:

    您可以使用mp3agic library

    一个用于读取 mp3 文件和读取/操作 ID3 标签(ID3v1 和 ID3v2.2 到 ID3v2.4)的 java 库。

    如果您的文件有 ID3 标签,您可以这样做:

    Mp3File mp3file = new Mp3File("src/test/resources/v1andv23tagswithalbumimage.mp3");
    
    if (mp3file.hasId3v1Tag()) {
       ID3v1 id3v1Tag = mp3file.getId3v1Tag();
       System.out.println("Track: "   + id3v1Tag.getTrack());
       System.out.println("Artist: "  + id3v1Tag.getArtist());
       System.out.println("Title: "   + id3v1Tag.getTitle());
       System.out.println("Album: "   + id3v1Tag.getAlbum());
       System.out.println("Year: "    + id3v1Tag.getYear());
       System.out.println("Genre: "   + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
       System.out.println("Comment: " + id3v1Tag.getComment());
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多