Java Reference
In-Depth Information
Figure 9-4. Displaying media metadata in the second version of the audio player
Loading Media
Loading media files from a predetermined jar resource or Internet URL is limiting if this audio player is going to grow
into a reasonably useful application. We support two ways of selecting content. First, we allow the user to select files
from the local file system using the JavaFX FileChooser . We also support dragging and dropping a file or URL onto the
audio player. Another issue that requires attention is scaling the application's source code. It is growing too large to be
contained in a single file and a single class. It is time to inflict a bit of architecture on our sample application.
We begin by isolating the code that deals with the Media , its associated metadata, and its MediaPlayer into a
separate class named SongModel . This class will expose a set of JavaFX properties (refer back to Chapter 4 if you need
a refresher) that represent the metadata of the current media and one that exposes the MediaPlayer instance. It also
defines a method for setting a media URL, which will trigger the creation of new Media and MediaPlayer instances.
This class is shown in Listing 9-10.
Listing 9-10. The SongModel Class
public final class SongModel {
private static final String DEFAULT_IMG_URL =
SongModel.class.getResource("resources/defaultAlbum.png").toString();
private static final Image DEFAULT_ALBUM_COVER =
new Image(DEFAULT_IMG_URL);
private final StringProperty album =
new SimpleStringProperty(this, "album");
private final StringProperty artist =
new SimpleStringProperty(this,"artist");
private final StringProperty title =
new SimpleStringProperty(this, "title");
private final StringProperty year =
new SimpleStringProperty(this, "year");
private final ObjectProperty<Image> albumCover =
new SimpleObjectProperty<>(this, "albumCover");
private final ReadOnlyObjectWrapper<MediaPlayer> mediaPlayer =
new ReadOnlyObjectWrapper<>(this, "mediaPlayer");
 
Search WWH ::




Custom Search