Audio in jME3

There are two ways to handle audio data: Short audio files are to be stored entirely in memory, while long audio files (music) is streamed from the hard drive as it is played. Place audio files in the assets/Sound/ directory of your project. jME3 supports Ogg Vorbis (.ogg) and Wave (.wav) formats.

Creating Audio Nodes

The main class to look at is com.jme3.audio.AudioNode. By default, a new audio node is buffered, i.e. JME loads the whole file into memory before playing:

AudioNode boom = new AudioNode(audioRenderer, assetManager, "Sound/boom.wav");

If it is a long file, you set the boolean to true to stream the audio.

AudioNode music = new AudioNode(audioRenderer, assetManager, "Sound/music.wav", true);

Setting AudioNode Properties

AudioNode MethodUsage
getStatus()Returns either Status.Playing, Status.Stopped, or Status.Paused.
setVolume(1)Sets the volume gain. 1 is the default volume, 2 is twice as loud, 0 is mute.
setPitch(1)Makes the sound play in a higher or lower pitch. Default is 1.
AudioNode MethodUsage
setLooping(false)Configures the sound that, if it is played, it plays once and stops. This is the default.
setLooping(true)Configures the sound that, if it is played, it plays repeats from the beginning, until stop() or pause() are called. Good for ambient background noises.
setPositional(false)
setDirectional(false)
All 3D effects switched off. This sound is global and comes from everywhere. Good for environmental ambient sounds and background music.
setTimeOffset(0.5f)Start playing the sound after waiting the given amount of seconds. Default is 0.
setMaxDistance(100f)Maximum distance the sound can be heard, in world units. Default is 20.
AudioNode MethodUsage
setPositional(true)
setLocalTranslation(new Vector 3f(0,0,0))
Activates 3D audio, the sound appears to come from a certain position, where it is loudest. Position the AudioNode in the 3D scene if you have setPositional() true. Position it with mobile players or NPCs.
setReverbEnabled(true)A 3D echo effect that only makes sense to use with moving positional AudioNodes. The reverb effect is influenced by the environment that the audio renderer is in. See "Setting Environment Properties" below.
AudioNode MethodUsage
setDirectional(true)
setDirection(new Vector3f(0,0,1))
Activates 3D audio. This sound can only be heard from a certain direction. Specify the direction and angle in the 3D scene if you have setDirectional() true. Good for noises that should not be heard through a wall.
setInnerAngle()
setOuterAngle()
Set the angle in degrees for the directional audio. The angle is relative to the direction. By default, both angles are 360° and the sound can be heard from all directions.

Play, Pause, Stop

You play, pause, and stop a node called myAudioNode by using the respective of the following three methods:

myAudioNode.play();
myAudioNode.pause();
myAudioNode.stop();

Note: Whether an Audio Node plays continuously or only once, depends on the Loop properties you have set above!

Setting Environment Properties

Optionally, You can choose from the following environmental presets from com.jme3.audio.Environment. This presets influence subtle echo effects that evoke associations of different environments in your users. You use it together with setReverbEnbaled(true) mentioned above.

EnvironmentdensitydiffusiongaingainHfdecayTimedecayHfreflGainreflDelaylateGainlateDelay
Garage1.00f1.0f1.0f1.00f0.90f0.5f0.751f0.0039f0.661f0.0137f
Dungeon0.75f1.0f1.0f0.75f1.60f1.0f0.950f0.0026f0.930f0.0103f
Cavern0.50f1.0f1.0f0.50f2.25f1.0f0.908f0.0103f0.930f0.0410f
AcousticLab0.50f1.0f1.0f1.00f0.28f1.0f0.870f0.0020f0.810f0.0080f
Closet1.00f1.0f1.0f1.00f0.15f1.0f0.600f0.0025f0.500f0.0006f

Activate the preset with setEnvironment(). E.g. in a dungeon environment:

audioRenderer.setEnvironment(new Environment.Dungeon));

A sound engineer can create a custom com.​jme3.​audio.Environment object and specify custom environment factors. Activate your custom environment settings in the Environment constructor:

audioRenderer.setEnvironment(
        new Environment( density, diffusion, gain, gainHf, decayTime, decayHf,
                reflGain, reflDelay, lateGain, lateDelay ) );

You can find more info about OpenAL and its advanced features here: OpenAL 1.1 Specification

view online version