Java Reference
In-Depth Information
The spectrumDataUpdate method takes four parameters. The timestamp parameter gives the time of the update
in seconds. The duration is the number of seconds for which the spectrum was computed. The duration should
normally be approximately equal to the value of MediaPlayer 's audioSpectrumInterval property. The magnitudes
array will hold the floating-point values corresponding to the magnitude of each spectrum band. The array length
will always equal the value of MediaPlayer 's audioSpectrumNumBands property. The value is in decibels and will be
between audioSpectrumThreshold and zero. It is always less than zero. Similarly, the phases array contains the phase
offset of each spectrum band. These arrays should be treated as read-only by the listener because the MediaPlayer
may reuse them between updates.
For our purposes, we are only trying to give the user a general idea of the spectrum content in each of the
equalizer bands. Therefore we need to be concerned only with the magnitudes array. We iterate through the array and
use the bucket counts to sum the magnitudes of the bands that correspond to each equalizer band. When we reach
the count for each bucket, the current sum is divided by the normalization factor and then passed as the value to the
corresponding SpectrumBar . Then the values are reset and the sum for the next band is calculated.
We now have the ability to create a custom set of EqualizerBand s and to display the magnitude of the frequencies
in each band as calculated from the audio signal that is currently being played. This gives a pretty nice equalizer
interface for the users of the application. We do need to ensure that we reinitialize our equalizer interface whenever a
new MediaPlayer is created (a new song is loaded). A listener attached to SongModel 's mediaPlayer property handles
this, as shown in Listing 9-27.
Listing 9-27. EqualizerView 's MediaPlayer Listener
private class MediaPlayerListener implements ChangeListener<MediaPlayer> {
@Override
public void changed(ObservableValue<? extends MediaPlayer> observable,
MediaPlayer oldValue, MediaPlayer newValue) {
if (oldValue != null) {
oldValue.setAudioSpectrumListener(null);
clearGridPane();
}
createEQInterface();
}
private void clearGridPane() {
for (Node node : ((GridPane)getViewNode()).getChildren()) {
GridPane.clearConstraints(node);
}
((GridPane)getViewNode()).getChildren().clear();
}
}
This inner class resides in EqualizerView . If the oldValue is not null, we set its spectrum listener to null to turn
off the spectrum calculations. Then we clear the GridPane and re-create the equalizer interface. This is necessary
because each MediaPlayer has its own AudioEqualizer instance that cannot be initialized or set from the outside.
It would also be wasteful to have the MediaPlayer compute the audio spectrum when it is not visible, such as when
the metadata view is being shown. We can attach another listener to our GridPane 's scene property to ensure that this
does not happen. Listing 9-28 shows the final version of the EqualizerView constructor with this addition highlighted.
Listing 9-28. The EqualizerView Constructor with the Scene Listener Declared
public EqualizerView(SongModel songModel) {
super(songModel);
 
Search WWH ::




Custom Search