Java Reference
In-Depth Information
AudioClip s are very useful for playing short, low-latency sound effects. If you need to play songs or background
music instead, the JavaFX media classes are a better choice.
Working with Media
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— 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 for the video to be displayed in the scene graph. The relationship among these three classes is
illustrated in Figure 9-3 . 2
Figure 9-3. The JavaFX 8 media classes
2 Naturally the invaders are initially greeted with fear and distrust. In a surprise plot twist, they turn out to be a competent and
helpful group of people.
 
Search WWH ::




Custom Search