Java Reference
In-Depth Information
In this listing, we are loading an episode of the Java Posse podcast from an Internet URL. Immediately after the
Media instance is created, we get a reference to its metadata map by calling the getMetadata method. We then attach
a MapChangeListener to the metadata map that makes a check to see if a new key/value pair was added to the map.
If so, the new key and value are passed to the handleMetadata helper method, which checks to see if it is a piece of
metadata that we are interested in handling. This is the code that is shown in bold in Listing 9-8.
Listing 9-8. Listening for Metadata from the Media Class
private Label artist;
private Label album;
private Label title;
private Label year;
private ImageView albumCover;
private void createMedia() {
try {
media = new Media(" http://traffic.libsyn.com/dickwall/JavaPosse373.mp3 " );
media.getMetadata().addListener((Change<? extends String, ? extends Object> ch) -> {
if (ch.wasAdded()) {
handleMetadata(ch.getKey(), ch.getValueAdded());
}
});
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setOnError(() -> {
final String errorMessage = media.getError().getMessage();
// Handle errors during playback
System.out.println("MediaPlayer Error: " + errorMessage);
});
mediaPlayer.play();
} catch (RuntimeException re) {
// Handle construction errors
System.out.println("Caught Exception: " + re.getMessage());
}
}
private void handleMetadata(String key, Object value) {
switch (key) {
case "album":
album.setText(value.toString());
break;
case "artist":
artist.setText(value.toString());
break;
case "title":
title.setText(value.toString());
break;
case "year":
year.setText(value.toString());
break;
Search WWH ::




Custom Search