Java Reference
In-Depth Information
How it works...
While playing audio only requires the use of the classes
Media
and
MediaPlayer
, playing
video requires an additional class called the
MediaView
. It is of type
Node
and can be used
to display the content of a video on the screen. Let's take a closer look at the code:
F
The MediaView
—the first major component to be initialized is the
MediaView
assigned to variable
player
. The code uses the
MediaView
instance to configure
the dimensions and the location where the video will be rendered. In order to control
playback, the code assigns the
player.mediaPlayer
property an instance of
MediaPlayer,
used to control playback. MediaPlayer is then assigned an instance
of
Media
(through the property
MediaPlayer.media
) to specify the location of the
video resource we want to playback.
F
The controls
—the GUI controls in this example work the exact same way as described
in
Playing audio with MediaPlayer
. We use a group of image icons to represent
playback functions play, pause, and stop. When the play icon is pressed, it is starts
playing the video by calling the
player.mediaPlayer.play()
function and
toggles itself to the pause icon. When the pause icon is pressed, it pauses the video
using function
player.mediaPlayer.pause()
. Finally, when the user presses the
stop button, it makes a call to
player.mediaPlayer.stop()
to stop playback and
toggles the play button back to the play icon.
There's more...
Processing video is expensive. The JavaFX
MediaView
class supports properties which can
be used to provide rendering-time hints to maximize playback performance. These Boolean
properties include:
F
compositable:Boolean—if
—if true, other nodes may overlay the
MediaView
node
using transparency.
F
preserveRatio:Boolean—if
—if true, the aspect ratio of the video is preserved when
the node is resized through the
fitWidth
or
fitHeight
property.
F
rotatable:Boolean
—when true, it allows the
MediaView
node to receive rotation
requests through the
rotate
property.
F
transformable:Boolean
—the node will only apply transformations through the
transforms:Transform[]
property when this is set to true.
See also
F
Accessing media assets
F
Playing audio with MediaPlayer