- jMonkeyEngine 3.0 Cookbook
- Rickard Edén
- 575字
- 2021-09-03 10:00:47
Adding some ambient audio
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.
How to do it…
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:
- We can find the audio node by right-clicking on a spatial, in this case the main scene node, and selecting Add Spatial and then Audio Node.
- Next, select it and look at the Properties window.
- The first important thing to look at is the Audio Data parameter. In the drop-down menu, the SDK will automatically show the files in the Sounds folder under Project Assets, so we should see Ocean Waves.ogg here. Unchecking the Positional checkbox means there will be no falloff in volume as you move around.
- Also check the Looping box to make sure the sound doesn't end when it's finished playing one time.
- It's currently not possible to hear the sound in the SDK itself, so we need to start an application to do so. Fortunately, only one line of code is needed to start the sound in our
simpleInitApp
method. The only catch here is that we need to cast thescene
object in anAudioNode
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();
- The sound we added is a very powerful sound and may be a bit overwhelming for our scene. Playing with the
Volume
property of theAudioNode
element can be used to tone down the effect a bit.
Tip
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.
How it works...
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.
- SEO實戰密碼
- Effective Python Penetration Testing
- 你不知道的JavaScript(中卷)
- Python數據可視化之Matplotlib與Pyecharts實戰
- JavaScript動態網頁開發詳解
- Symfony2 Essentials
- PLC應用技術(三菱FX2N系列)
- Python3.5從零開始學
- Flowable流程引擎實戰
- CodeIgniter Web Application Blueprints
- Go語言從入門到精通
- Tableau Desktop可視化高級應用
- HTML5移動前端開發基礎與實戰(微課版)
- 零基礎學SQL(升級版)
- SQL Server 2014 Development Essentials