How to get metadata from Media objects

2019-06-14 05:25发布

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;
}





}

In my controller class

// 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.

1条回答
Root(大扎)
2楼-- · 2019-06-14 06:08

As per the documentation (my emphasis):

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.

So you can do something like this:

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);

}
查看更多
登录 后发表回答