gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:51 24 4
gpt4 key购买 nike

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

谢谢。

最佳答案

您可以使用mp3agic library .

A java library for reading mp3 files and reading / manipulating the ID3 tags (ID3v1 and ID3v2.2 through 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());
}

关于java - 使用 java 录制流时获取 MP3 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com