Java Reference
In-Depth Information
final Duration seekTo = mediaPlayer.getTotalDuration().multiply(pos);
seekAndUpdatePosition(seekTo);
}
}
}
PositionListener is a ChangeListener that waits for an old value of true (the value was changing) and a new
value of false (the value is not changing anymore). When those conditions exist, we know the user has finished
dragging the slider and it is time to seek the new position. The new position is calculated by multiplying the slider's
new value, which you recall can range from 0.0 to 1.0, by the MediaPlayer 's totalDuration to give the new Duration
we pass to the seek method. This value is passed to the same seekAndUpdatePosition helper method that was shown
in Listing 9-18, which handles the details of the call to the seek method.
Controlling Volume
The MediaPlayer class has two properties that control the volume of the audio playback. The volume property has
a range that goes from 0.0 (mute) to 1.0 (maximum volume). This volume setting does not affect the master volume
of the computer on which the audio is playing; it controls only the volume used by the MediaPlayer . The default
value for the volume property is 1.0. There is also a Boolean property named mute . When this variable is set to true,
the volume of the playback will be muted. Although playback continues, no sound will be heard. This is effectively
equivalent to setting the volume property to 0.0, but muting playback allows you to cut out the sound and easily
restore it later to its previous level without needing to read and reset the volume value.
The volume control in the audio player application is a Slider with a value that can range from 0.0 to 1.0. Simply
establishing a bidirectional binding between the Slider 's value property and the MediaPlayer 's volume property will
allow the user to control the volume of the playback.
volumeSlider.valueProperty().bindBidirectional(mediaPlayer.volumeProperty());
That concludes our look at the player controls in our sample application. We have not shown the code that listens
to changes in the mediaPlayer property itself to add and remove listeners cleanly. If you are interested in viewing the
full source of the PlayerControlsView , it can be found in the AudioPlayer3 project in the topic's Chapter 9 example
code. Figure 9-6 shows what the audio player looks like at this point.
Figure 9-6. The audio player's playback controls. “Just take a point called z in the complex plane. . . .”
 
Search WWH ::




Custom Search