Javafx 元数据
Javafx metadata
一段时间以来,我一直试图了解如何从媒体中获取元数据,但目前没有任何效果。我有 class 歌曲,其中有 SimpleStringProperties,如标题、艺术家等。我尝试在 class 构造函数中为它们设置值:
private final SimpleStringProperty title;
private final SimpleStringProperty artist;
public Song(String path) {
this.song = new MediaPlayer(new Media(path));
this.artist = new SimpleStringProperty(this, "artist");
this.title = new SimpleStringProperty(this, "title");
this.song.setOnReady(() -> {
title.set(song.getMedia().getMetadata().get("title").toString());
artist.set(song.getMedia().getMetadata().get("artist").toString());
});
}
然后我尝试在 fxml 控制器中制作一首新歌:
Song song = new Song(path);
System.out.println(song.getTitle());
System.out.println(song.getArtist());
我在控制台看到了
null
null
我知道在 setOnReady()
方法中它会正确显示标题和艺术家。我有一个 Platform.runLater()
的解决方案,但是当有更多新歌时它就不能正常工作了。我读过一些关于 synchronized()
的内容,但我不知道如何使用它。我正在等待一些解决方案。提前致谢:)
您在调用处理程序之前调用 getTitle()
和 getArtist()
(即在 MediaPlayer
准备好之前)。
据推测,您并不是真的想将这些显示到系统控制台,而只是为了测试而这样做。试试像
Label titleLabel = new Label();
Label artistLabel = new Label();
Song song = new Song(path);
titleLabel.textProperty().bind(song.titleProperty());
artistLabel.textProperty().bind(song.artistProperty());
然后在您的 UI 中显示这些标签。当数据可用时,它们将自动更新。
一段时间以来,我一直试图了解如何从媒体中获取元数据,但目前没有任何效果。我有 class 歌曲,其中有 SimpleStringProperties,如标题、艺术家等。我尝试在 class 构造函数中为它们设置值:
private final SimpleStringProperty title;
private final SimpleStringProperty artist;
public Song(String path) {
this.song = new MediaPlayer(new Media(path));
this.artist = new SimpleStringProperty(this, "artist");
this.title = new SimpleStringProperty(this, "title");
this.song.setOnReady(() -> {
title.set(song.getMedia().getMetadata().get("title").toString());
artist.set(song.getMedia().getMetadata().get("artist").toString());
});
}
然后我尝试在 fxml 控制器中制作一首新歌:
Song song = new Song(path);
System.out.println(song.getTitle());
System.out.println(song.getArtist());
我在控制台看到了
null
null
我知道在 setOnReady()
方法中它会正确显示标题和艺术家。我有一个 Platform.runLater()
的解决方案,但是当有更多新歌时它就不能正常工作了。我读过一些关于 synchronized()
的内容,但我不知道如何使用它。我正在等待一些解决方案。提前致谢:)
您在调用处理程序之前调用 getTitle()
和 getArtist()
(即在 MediaPlayer
准备好之前)。
据推测,您并不是真的想将这些显示到系统控制台,而只是为了测试而这样做。试试像
Label titleLabel = new Label();
Label artistLabel = new Label();
Song song = new Song(path);
titleLabel.textProperty().bind(song.titleProperty());
artistLabel.textProperty().bind(song.artistProperty());
然后在您的 UI 中显示这些标签。当数据可用时,它们将自动更新。