Java Reference
In-Depth Information
Listing 8.5
MediaPlayer
var mediaPlayer:MediaPlayer = MediaPlayer {
volume: 0.5
autoPlay: false
onError: function(e:MediaError) {
println("got a MediaPlayer error : {e.cause} {e}");
mediaPlayer.stop();
mediaPlayer.media = null;
}
onEndOfMedia: function() {
println("reached end of media");
mediaPlayer.play();
mediaPlayer.stop();
mediaPlayer.media = null;
}
};
This merely creates the player; to use it, you have to set the
media
variable to a
Media
object that points to a source, then invoke the
play()
,
pause()
, or
stop()
functions to control play. There are several instance variables that you may set on
the
MediaPlayer
. If
autoPlay
is true, playing will start right away. The variables
balance
,
fader
, and
volume
control the left-to-right setting for the audio, the
front-to-back audio setting, and the sound volume. The
mute
variable toggles the
sound off or on. The
onError
and
onEndOfMedia
functions are invoked when an
error occurs or the end of the media is reached. The
rate
specifies the play
speed, 1.0 being normal speed. There are also functions and variables to report
status, buffering state, and start, stop, and current time settings.
For audio playback, this is all you need to use audio in your application. How-
ever, for video playback, you need a view node, the
javafx.scene.media
.MediaView
.
Listing 8.6 shows how to create a
MediaView
that can display the
video.
Listing 8.6
MediaView
mv = MediaView {
translateX: bind (scene.width - mv.layoutBounds.width)/2
translateY: bind (scene.height -
mv.layoutBounds.height - bottomBorder)/2
mediaPlayer: bind mediaPlayer
preserveRatio: true
fitWidth: bind scene.width - 4 * borderWidth
fitHeight: bind scene.height - 4 * borderWidth -
bottomBorder
Search WWH ::
Custom Search