Incorporating Media Assets Into JavaFX Applications


1 Introduction to JavaFX Media

The active growth of media content on the web has made video and audio an essential part of rich Internet applications. The idea of broadening the horizons of traditional media usage led to the creation of the JavaFX media functionality that is available through a set of Java APIs. The package enables developers to create media applications that provide media playback in the desktop window or within a web page on supported platforms.

Figure 1-1 demonstrates a variety of possible media usages in JavaFX applications.

Figure 1-1 Samples of Media Usages

Description of Figure 1-1 follows
Description of "Figure 1-1 Samples of Media Usages"

The operating systems and Java Runtime Environments (JREs) supported by JavaFX media features are the same as those listed in the JavaFX SDK System Requirements document. Refer to the JavaFX Release Documentation page for the appropriate system requirements information for the version of JavaFX SDK you are using.

Supported Media Codecs

The formats currently supported are the following:

  • Audio: MP3; AIFF containing uncompressed PCM; WAV containing uncompressed PCM; MPEG-4 multimedia container with Advanced Audio Coding (AAC) audio

  • Video: FLV containing VP6 video and MP3 audio; MPEG-4 multimedia container with H.264/AVC (Advanced Video Coding) video compression


You may not integrate the On2 VP6 video decoder in the design of a semiconductor or register transfer level (RTL) or any other similar level necessary for development of semiconductor implementation of the On2 VP6 video decoder.

The FLV container is supported by the media stack on the platforms supported by the JavaFX SDK. A single movie encoded in this format works seamlessly on supported platforms. Standard FLV MIME settings are required on the server side to enable media streaming.

The MPEG-4 multimedia container is also supported on all operating systems supported by the JavaFX SDK. On the Mac OS X and Windows 7 platforms, playback will be functional without requiring additional software. However, the Linux operating system and versions of Windows older than Windows 7 require the installation of readily available third party software packages, as documented in the JavaFX System Requirements. AAC and H.264/AVC decoding have certain platform-dependent limitations, as described in the JavaFX Release Notes.

Decoding of some audio and video compression types relies on operating system-specific media engines. The JavaFX media framework does not attempt to handle all multimedia container formats and media encodings supported by these native engines. Instead, the framework attempts to provide equivalent and well-tested functionality across all platforms on which JavaFX is supported.

Some of the features supported by the JavaFX media stack include the following:

  • FLV container with MP3 and VP6

  • MP3 audio

  • MPEG-4 container with either AAC, H.264, or both

  • HTTP, FILE protocol support

  • Progressive download

  • Seeking

  • Buffer progress

  • Playback functions (Play, Pause, Stop, Volume, Mute, Balance, Equalizer)

HTTP Live Streaming Support

With the addition of HTTP live streaming support, you can now download the playlist file and playback video or audio segments using JavaFX Media. Media players are now able to switch to alternate streams, as specified in the playlist file and based on network conditions. For a given stream, there is a playlist file and a set of segments into which the stream is broken. The stream can be either an MP3 raw stream or an MPEG-TS containing multiplexed AAC audio and H.264 video. The stream can be played on demand when the stream is a static file or played live when the stream is actually a live feed. In both cases, the stream can adjust its bit rate and for video, its resolution can be adjusted.

Creating a Media Player

The JavaFX media concept is based on the following entities.

  • Media – A media resource, containing 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. Figure 1-2 shows the classes that reside in the package. These classes are interdependent and are used in combination to create an embedded media player.

Figure 1-2 Classes in the Package

Description of Figure 1-2 follows
Description of "Figure 1-2 Classes in the Package"

The MediaPlayer class provides all the attributes and functions needed to control media playback. You can either set the AUTO_PLAY mode, call the play() function directly, or explicitly specify the number of times that the media should play. The VOLUME variable and the BALANCE variable can be used to adjust the volume level and left-right settings, respectively. The volume level range is from 0 to 1.0 (the maximum value). The balance range is continuous from -1.0 on the far left, 0 at the center, and 1.0 at the right.

The play(), stop(), and pause() functions control media playback. Additionally, a bundle of functions handles specific events when the player does one of the following:

  • Buffers data

  • Reaches the end of media

  • Stalls because it has not received data fast enough to continue playing

  • Encounters any of the errors defined in the MediaErrorEvent class

The MediaView class extends the Node class and provides a view of the media being played by the media player. It is responsible mostly for effects and transformations. 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 the package, see the API documentation.