Java Reference
In-Depth Information
Error Handling
The Media class has two properties, error and onError , that help you deal with any errors that occur during
playback. You can manually check for errors by calling the getError method, which will return a MediaException
object from the underlying errorProperty if an error has occurred. You can listen for errors by attaching an
InvalidationListener or ChangeListener to the errorProperty of the Media class. As a convenience, you can simply
pass a Runnable to the setOnError method to achieve the same result. The Runnable 's run method will be called
if an error occurs. It is safe to update the scene graph from the run method inasmuch as it is called on the JavaFX
application thread.
Media instances are rarely used on their own. Normally they will be passed to a MediaPlayer as a constructor
argument. The MediaPlayer class replicates the error and onError properties of Media , and all Media errors are
forwarded to the error property and the onError callback of the enclosing MediaPlayer instance. Therefore, you only
need to use the MediaPlayer error properties to catch errors from both the MediaPlayer and its underlying Media
object all in one place.
Exceptions, including MediaException s, can also occur during the construction of a Media or MediaPlayer
instance. Obviously these exceptions will not be available in the object's error property because the object does not
exist yet. If you want to be thorough in your error handling, you should also enclose your constructors in try-catch blocks
in addition to setting MediaPlayer 's onError handler. This error-handling technique is demonstrated in Listing 9-7.
Listing 9-7. Error Handling with the Media Classes
try {
final URL resource = getClass().getResource("resources/keeper.mp3");
final Media media = new Media(resource.toString());
final MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setOnError(() -> {
final String errorMessage = media.getError().getMessage();
...
});
mediaPlayer.play();
} catch (RuntimeException re) {
// Handle Media and MediaPlayer construction errors...
}
Displaying Metadata
Most media file formats have the ability to embed metadata , data that describe the song or video. MP3 files, for
instance, have ID3 tags that can be used to identify the artist, album, track number, and even the year that a song was
released. Many MP3 files also have embedded images of the artist, the album cover, or a logo (in the case of podcasts
and such). The JavaFX Media class reads these metadata and presents them to the developer in the form of an
ObservableMap of key/value pairs. The keys are String s that identify the metadata and the values are Objects , most
likely a String or an Image .
Although there is no guarantee as to which metadata are present in any given media file, there are a few fields
that are very common: artist, title, album, and year. In addition, if any image is embedded in a file's metadata, the
Media class will give you access to it using the “image” key. Listing 9-8 shows you how to add the ability to receive
metadata notifications when a new Media object is created.
 
Search WWH ::




Custom Search