Audio is an extremely important moodsetter in games, and any other cross-media product, which is often overlooked. Bad audio can just as easily break immersion as good audio can improve it.
We're going to add some ambient audio to our scene to help set the mood. Since the sky box we use is a rather gloomy and watery scene, we're going to add the sound of ocean waves crashing against the shore.
Ambient sounds can either be sounds you hear throughout a whole scene, such as the buzzing of traffic in a city, or local to a specific place, the sound of a waterfall, and so on. In this case, we can picture our scene as a small island, and thus the waves should be heard wherever you go.
As it happens, there is a suitable .ogg
file in the Environments folder inside Sound
. If we have added the jme3-test-data
library to our project, we can access it easily.
The SDK can handle both .ogg
or uncompressed .wav
files. The .ogg
format is open and free, meaning you won't need any license to use it. This is not necessarily the case with other compression types.
If we've made the previous recipes, we might already have seen the audio node. The following steps will help show us how to add one to the scene:
simpleInitApp
method. The only catch here is that we need to cast the scene
object in an AudioNode
instance first. After having loaded the scene, add the following lines of code:Node scene = (Node) assetManager.loadModel ("Scenes/TestScene.j3o"); rootNode.attachChild(scene); ((AudioNode)scene.getChild("AudioNode")).play();
Volume
property of the AudioNode
element can be used to tone down the effect a bit.Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The AudioNode
element has a position in the 3D world since it extends Spatial
and can hence be made to be heard only from certain places. It can also easily be made to follow objects around. In addition to volume and falloff, audio can also be modified during runtime by area effects such as reverb.
To learn more about how effects can be used to modify audio, check out Chapter 9, Taking Our Game to the Next Level.
18.226.181.57