Java Reference
In-Depth Information
MediaPlayer
also exposes a balance property, which controls the left-to-right balance of the audio. The range
of the variable is from -1.0 (playing from the left speakers only) to 1.0 (playing from the right speakers only). If the
audio that is playing has only one track (mono), that track will be played on both speakers and the balance control
will act as a volume control, allowing you to adjust how loud the left or right speaker is playing. If the audio has two
tracks (stereo), the left track is played on the left speaker and the right track is played on the right speaker. In this case
the balance control will allow you to fade back and forth between the left and right audio tracks. The default value of
the balance variable is 0.0. The audio player application does not provide any controls to adjust the balance; we have
mentioned it here only for completeness.
Repetition
Another
MediaPlayer
feature not used by the audio player sample application, but that certainly bears mentioning,
is the ability to repeat the playback of your media. You can set the playback to repeat itself a certain number of times
or to repeat forever. This behavior is controlled by the
cycleCount
property. The number of playback cycles that have
been completed is available in the
currentCount
property. When the end of the media is reached,
currentCount
is incremented and, if it is less than
cycleCount
, playback will begin again automatically. The
onRepeat
callback
function is called whenever playback is repeated. The usual call to
onEndOfMedia
will not be made until after the final
repetition. Setting
cycleCount
to the value
MediaPlayer.INDEFINITE
will cause the playback to loop indefinitely.
The
startTime
and
stopTime
properties in the
MediaPlayer
class can affect where the media start and stop
playing. As their names indicate, both of these properties are
Duration
types. The
startTime
property is initialized to
0 milliseconds, whereas
stopTime
is initialized to the value of
totalDuration
. If a
stopTime
is set, playback will stop
or repeat when that time is reached. On a related note, the
cycleDuration
property is a read-only property that gives
you the current difference between the
startTime
and
stopTime
.
The ability to set the playback to repeat would be a handy feature for the audio player to have. It is not currently
implemented in the sample application, but adding it would be a good exercise for the enthusiastic reader.
Audio Equalization
Two very cool features of the JavaFX media API are the abilities to create an audio equalizer and to view the live
spectrum of the audio as it's being played. The equalizer allows you to boost or attenuate the audio at certain
frequencies. This change is then visible if you are visualizing the audio's frequency spectrum; the two features work
together to give you or your users ultimate control of the playback experience.
Each
MediaPlayer
creates an
AudioEqualizer
instance that you can access using the
getAudioEqualizer
method.
The
AudioEqualizer
class has an enabled property that you can use to enable or disable the equalizer. It also exposes
an
ObservableList
of
EqualizerBand
instances. Each
EqualizerBand
has
bandwidth
,
centerFrequency
, and
gain
properties. The
bandwidth
and
centerFrequency
properties let you define the range of frequencies that are affected
by the band, and the
gain
property boosts or attenuates those frequencies. All three of those properties are mutable.
When you get a reference to the
MediaPlayer
's equalizer, it will already have a number of
EqualizerBands
defined.
You can modify the settings of those bands, or remove some or all of them and replace them with your own bands.
Listing 9-22 shows the start of the
EqualizerView
class. This new view is used to display an audio equalizer and
a live spectrum. It is added in the AudioPlayer4 example project in the topic's source code.
Listing 9-22.
The
EqualizerView
Class
public class EqualizerView extends AbstractView {
private static final double START_FREQ = 250.0;
private static final int BAND_COUNT = 7;
public EqualizerView(SongModel songModel) {
super(songModel);
createEQInterface();
}
Search WWH ::
Custom Search