Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 21 ■ Questions and answers: Finishing the setup Methods and digital audio

I’ve combined all three Audacity File ➤ Save As dialogs into one in Figure 21-33 to save on space;
we have a lot that we need to look at during this chapter, in both NetBeans and Audacity. The first panel,
numbered 1, shows your 44.1 KHz 16-bit file being saved as spinner.wav in the NetBeansProjects/
JavaFXGame/src/ folder. The second portion of the figure shows a 22.05 KHz 16-bit version being saved
as spinner22.wav, and the third portion of the figure shows the 11.025 KHz 16-bit version being saved as
spinner11.wav. The file sizes for these three audio assets come to about 658KB, 329KB, and 165KB. Since
these are 16-bit PCM .wav files, the amount of memory used to store the file also happens to be the amount
of system memory used to deploy the file for use in your games.


Now we can continue the AudioClip instantiation statement and use a new audio sample in our game logic!

Use toExternalForm() to Load a URI Reference as a String Object


Now we can add this spinner.wav file name in the getResource() method and then chain that method call
to the toExternalForm() method call, which converts the spinner.wav audio resource to the external (URI
String) form that is required by the AudioClip constructor method. Be sure to add the root (/) forward slash
to your spinner.wav so it can be seen in the root source (/src) folder. The Java code for this statement is
shown under construction in Figure 21-34:


spinnerAudio = new AudioClip( JavaFXGame.class.getResource("\spinner.wav").toExternalForm() );


Figure 21-32. Reduce the sample format by another 100 percent from 44.1 to 11.025 KHz, using the Project
Rate drop-down menu


Figure 21-33. Use Audacity’s File ➤ Save function to export 44, 22, and 11 Hz, 16-bit audio versions to /
JavaFXGame/src

Free download pdf