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.
As per the documentation (my emphasis):
So you can do something like this: