Creating a Media Player


Watch the screencast



The JavaFX Media concept is based on the following entities:

  • Media – A media resource, contains information about the media, such as its source, resolution, and metadata
  • MediaPlayer – The key component providing the controls for playing media
  • MediaView – A Node object to support animation, translucency, and effects

Each element of the media functionality is available through the JavaFX API. The following diagram shows the classes that reside in the javafx.scene.media package. These classes are interdependent and are used in combination to create an embedded media player.

Overview of the JavaFX Media API Figure 1: JavaFX Media API

The MediaPlayer class provides all the attributes and functions needed to control media playback. The media instance variable 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. Use the timers instance variable to preset which action will be performed when the media reaches the specific time. The volume and the balance control the audio output: the volume level and left-right settings. While the volume level might change from 0 to 1.0 (the maximum value), the balance 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, a bundle of functions handles the following specific events:

  • The player buffers data.
  • The player reaches the end of media.
  • The player stalls because it has not received data fast enough to continue playing.
  • The player encounters any of the errors defined in the MediaError class.

The MediaView class is responsible mostly for effects and transformation. Its mediaPlayer instance variable 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 player to be rotated.

For more information about javafx.scene.media package, refer to the API documentation.


Rate This Article
Discussion

We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

 

English
日本語
한국어
简体中文
русский
Português do Brasil