The JMC concept regards the player and the media as separate objects. This solution is determined by typical scenarios of use.
JMC provides two options for developers to use a player either with or without user interface controls. Both player components are implemented lightweight and extend the JComponent class. While the first component is a simple player with customary UI controls, the second one is an embedded player providing all the required functionality through its API.
The table below shows the difference between those two types.
Player with UI controls | Player without UI controls |
---|---|
Provides a fully integrated media player with a complete UI to control the playback. Possesses relatively limited functionality. | Provides a video panel without any user interface elements. Extended functionality is available through the API. |
|
|
Obtains the current volume level, the mute state of the signal, the auto-repeat mode. | Obtains the duration of the current media, the playback rate, the URL of the current media, the current media time position, the current volume level, the mute state of the signal. |
Sets the auto-repeat mode, the volume level. | Sets the media rate, the auto-repeat mode, the current media time position, the volume level |
The media object provides detailed information about video and audio stream such as tracks, duration, and meta data. Media events handle the predefined state changes in the media player. Moreover, a set of exceptions is introduced to notify an application of an error that may occur if the media has been corrupted or is not supported, or if the requested media file is unaccessible or unavailable.
This functionality has been wrapped in JavaFX Script classes and made available in the javafx.scene.media package. The JavaFX Media Player concept operates with the following entities:
These classes are interdependent and used in combination to create an embedded media player.
![]() Figure 1: Media Classes |
MediaPlayer is the key class of the package and it provides all the attributes and functions needed to control media playback. The media attribute defines the source Media object to be played. You can either set the auto play mode or explicitly specify the number of times the media should repeat. To preset what action will be performed when the media reaches the specific time, use the timers attribute. The volume and balance attributes control the audio output: the volume level and left-right settings. While the volume level may change from 0 to 1.0 (the maximum level), the balance value ranges continuously from -1.0 on the far left, 0 at the center, and 1.0 at the right.
The play() and pause() functions control media playback. Additionally, there is a bundle of functions to handle the following specific events: the player is buffering data, the player reaches the end of media, the player has stalled because it was not receiving data fast enough to continue playing, or any of the errors defined in the MediaError class.
The MediaView class is responsible mostly for effects and transformation. Its mediaPlayer attribute specifies the MediaPlayer object by which the media is being played. Other boolean attributes serve to apply the particular effect provided by the Node class. For example, to enable the media to be rotated, set the rotatable attribute to true and define its rotate attribute.
The next lesson gives you more practical advice on how to integrate media in your application.