一段时间以来,我一直试图了解如何从媒体中获取元数据,但到目前为止,没有任何东西起作用。我有类歌,其中有SimpleStringProperties,如标题、艺术家等。我试图在类构造函数中为它们设置值:
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()的文章,但我不知道如何使用它。我正在等待一些解决办法。(预先谢谢:)
发布于 2015-09-11 14:15:10
在调用处理程序之前(即在getTitle()准备就绪之前),您正在调用MediaPlayer和getArtist()。
想必,您并不想将这些显示到系统控制台上,而只是为了测试。试着做些像
Label titleLabel = new Label();
Label artistLabel = new Label();
Song song = new Song(path);
titleLabel.textProperty().bind(song.titleProperty());
artistLabel.textProperty().bind(song.artistProperty());然后在UI中显示这些标签。当数据可用时,它们将自动更新。
https://stackoverflow.com/questions/32523552
复制相似问题