Java Reference
In-Depth Information
new
MediaPlayer
instance. The following code checks for a prior media player to
be stopped:
if (mediaPlayer != null) {
mediaPlayer.stop();
}
So, here is where you create a
MediaPlayer
instance. A
MediaPlayer
object
is responsible for controlling the playing of media objects. Notice that a
MediaPlay-
er
will treat sound or video media the same in terms of playing, pausing, and stopping
media. When creating a media player, you specify the
media
and
audioSpec-
trumListener
attribute methods. Setting the
autoPlay
attribute to
true
will
play the audio media immediately after it has been loaded. The last thing to specify on
the
MediaPlayer
instance is an
AudioSpectrumListener
. So, what exactly is
this type of listener, you say? Well, according to the Javadoc, it is an observer receiving
periodic updates of the audio spectrum. In layman's terms, it is the audio media's
sound data such as volume, tempo, and so on. To create an instance of an
Audi-
oSpectrumListener
, you create an inner class that overrides the method
spec-
trumDataUpdate()
. You could have also used a lambda expression here; the ex-
ample uses the inner class to provide better insight into the functionality.
Table 16-2
lists all the inbound parameters for the audio spectrum listener's method. For more de-
tails, refer to the Javadoc at
http://docs.oracle.com/javase/8/javafx/
date()
Inbound Parameters
In the example, randomly colored circle nodes are created, positioned, and placed
on the scene based on the variable phases (array of floats). To draw each colored circle,
the circle's center X is incremented by five pixels and the circle's center Y is added