Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The JavaFX media classes make it very easy to work with audio and video files by simply treating them as two different types of media. Therefore, the basic playback of an audio or video file can be accomplished using only a few lines of (nearly identical) code. On the other hand, there is also some depth to the API that allows you to go beyond simple playback to enhance the experience for your users. In the remainder of this chapter, we start with simple audio playback and then show you how to tap into the power of this API and take your media-based applications to the next level.
The JavaFX media classes are located in the javafx.scene.media package. There are three main classes named Media, MediaPlayer, and MediaView as well as a number of supporting classes. The Media class encapsulates the audio or video resource to be played. The string representation of the URI identifying this resource is passed to the Media constructor as its only argument. The Media object exposes properties that describe the media’s width and height (when dealing with video), its duration, and two properties related to error handling. Once constructed, a Media object can be passed to the constructor of a MediaPlayer instance, which provides a set of methods for controlling the playback of the audio or video resource. And finally, a MediaPlayer can be passed to the constructor of a MediaView node in order for the video to be displayed in the scene graph. The relationship among these three classes is illustrated in Figure 8-3.2