Java Reference
In-Depth Information
public function isPlaying():Boolean{
return soundHelper.isPlaying();
In Listing 9-4 we can see the class SoundPlayer . The class SoundPlayer is intended to wrap a
SoundHelper and provide a JavaFX-style interface to any application that requires the feature of
SoundHelper . We can see SoundPlayer implements the interface Observer and thus has an update
function. It is very simple for JavaFX classes to extend Java interfaces; the only real difference is in the
syntax of declaring the function. In the init function, we can see that SoundPlayer creates a new
SoundHelper and then registers itself as an observer. Now any time the levels change in the SoundHelper ,
the update function of SoundPlayer will be called.
Looking at the update function of SoundPlayer , we can see that the levels in SoundHelper are copied
into the sequence levels of class SoundPlayer . But notice that the for loop that does the copying is
actually performed in a function that is passed the static function FX.deferAction . The function
FX.deferAction is a utility function that causes any function passed into it to be called by the JavaFX
event thread. This is important because this allows other JavaFX objects to bind to the sequence levels
in a reliable way.
In fact, SoundPlayer has a number of other variables, which are bound to levels such as hiChannels ,
midChannels , and lowChannels . These variables are simply aggregates of the values in levels and will be
used later to allow audio visualization to bind to just the high, middle, or low parts of the song.
SoundPlayer also has a number of functions that simply wrap methods on the soundHelper ; this is
done to make SoundPlayer a complete package and prevents developers who use SoundPlayer from
needing to know anything about SoundHelper and the whole Java side of things.
One last thing to note is how simple it is for JavaFX classes to make calls to Java objects. On the
JavaFX side, the Java object is created as normal, and method calls are made just like they were native
JavaFX objects. Calling JavaFX functions from Java is a bit trickier; there are particulars with the
differences in JavaFX primitive types and Java's primitive types that can confound any developer. The
trick here was to have the JavaFX class implement a Java interface that ensures that the types used in the
function calls are going to be familiar from the Java perspective.
Audio Visualizations
Now that we have a nice JavaFX interface for our sound processing code, we can start using SoundPlayer
in an example application that will illustrate how easy it is to create compelling audio visualizations in
JavaFX. Figure 9-1 shows the sample application we will be talking about.
In Figure 9-1 we can see scene composed of a control for starting and pausing the music, as well as a
control bar where we can change which part of the song is playing. There are also three check boxes that
control which three of our example effects are displayed. In this screenshot, all three are displayed. Let's
start by looking at Main.fx and how this example was set up (Listing 9-5).
Listing 9-5. Main.fx
var soundPlayer = SoundPlayer{
url: "{__DIR__}media/01 One Sound.mp3";
var bars = Bars{
translateX: 50
Search WWH ::

Custom Search