使用java录制流时获取MP3标签

时间:2017-09-10 10:36:48

标签: java audio io streaming mp3

我目前有这个代码,它成功地记录了一个(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标签的答案,但不是从流音频中获取。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用mp3agic library

  

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

如果你的文件有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());
}