如何从Media对象获取元数据 [英] How to get metadata from Media objects

查看:167
本文介绍了如何从Media对象获取元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX中制作一个MP3播放器,到目前为止已经让它加载歌曲和播放,但现在我想将Media对象中的元数据显示到tableview。我有一个作为模型类的歌曲类,在我的控制器类中,我有一个标签,我正在测试是否可以显示元数据,但它总是为空。

I am trying to make a mp3 player in JavaFX and so far have gotten it to load songs and play, but now I want to display the metadata from the Media object to a tableview. I have a song class which acts as a model class and in my controller class I have a Label I am testing to see if I can get the metadata to display, but it always comes as null.

package application;

import java.io.File;

import javafx.collections.MapChangeListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class Song {
private File file;
private String title;
private String artist;
private String album;
private Media music;
private MediaPlayer mp;

public Song(File file) {
    music = new Media(file.toURI().toString());
    mp = new MediaPlayer(music);
    getMeta();
    artist = (String) mp.getMedia().getMetadata().get("artist");
    title = (String) music.getMetadata().get("title");
    album = (String) music.getMetadata().get("album");
    //artist = "test";
    //album = "test";
    //title = "test";
}


public void play() {
    mp.play();
}

public void pause() {
    mp.pause();
}

public void stop() {
    mp.stop();
}

public String getTitle(){
    return title;
}

public String getArtist(){
    return artist;
}

public String getAlbum(){
    return album;
}





}

在我的控制器类中

// Event Listener on Button[#loadBtn].onAction
@FXML
public void loadFile(ActionEvent event) {
    Node source = (Node) event.getSource();
    Window theStage = source.getScene().getWindow();
    //set fileChooser filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("MP3 files", "*.mp3");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Select MP3 files");
    //File file = fileChooser.showOpenDialog(theStage);
    //mySong = new Song(file);
    list = fileChooser.showOpenMultipleDialog(theStage);
    if(list!=null){
        for(File x: list) {
            mySong = new Song(x);
        }
    }
    label.setText(mySong.getTitle());
}

在最后一行,我正在测试是否正确检索了元数据,它是总是空的。我将标签设置为最初说测试,所以我会知道它是否正在进行任何更改而且确实如此,但不是我想要的。

In the last line I am testing if the metadata was retrieved correctly and it is always null. I have the label to be set as initially to say "test" so I would know if it was making any changes and it does, but not to what I want.

推荐答案

根据文档(我的重点):


有关媒体的信息,如持续时间,元数据,曲目和视频分辨率可以从Media实例获得。 媒体信息是异步获取的,因此在实例化类之后不一定立即可用。如果实例已与MediaPlayer关联且该播放器已转换为MediaPlayer.Status,则所有信息都应可用。就绪状态。要在添加元数据或轨道时收到通知,观察者可以分别使用getMetadata()和getTracks()返回的集合进行注册。

Information about the media such as duration, metadata, tracks, and video resolution may be obtained from a Media instance. The media information is obtained asynchronously and so not necessarily available immediately after instantiation of the class. All information should however be available if the instance has been associated with a MediaPlayer and that player has transitioned to MediaPlayer.Status.READY status. To be notified when metadata or Tracks are added, observers may be registered with the collections returned by getMetadata()and getTracks(), respectively.

所以你可以这样做:

public Song(File file) {
    music = new Media(file.toURI().toString());

    music.getMetadata().addListener((Change<? extends String, ? extends Object> c) -> {
        if (c.wasAdded()) {
            if ("artist".equals(c.getKey())) {
                artist = c.getValueAdded().toString();
            } else if ("title".equals(c.getKey())) {
                title = c.getValueAdded().toString();
            } else if ("album".equals(c.getKey())) {
                album = c.getValueAdded().toString();
            }
        }
    });

    mp = new MediaPlayer(music);

}

这篇关于如何从Media对象获取元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆