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(assetManager, "Sound/boom.wav");

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

AudioNode music = new AudioNode(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(…)
Activates 3D audio: The sound appears to come from a certain position, where it is loudest. Position the AudioNode in the 3D scene, or move it with mobile players or NPCs.
setReverbEnabled(true)A 3D echo effect that only makes sense to use with 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(…)
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. Use this to restrict noises that should not be heard, for example, through a wall.
setInnerAngle()
setOuterAngle()
Set the angle in degrees for the directional audio. The angle is relative to the direction. Note: 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!

You can also start playing an instance of this AudioNode. Use the playInstance() method if you need to play the same AudioNode multiple times, possibly simulatenously. Note that changes to the parameters of the original AudioNode do not affect the instances that are already playing!

myAudioNode.playInstance();

The Listener

The default listener object is the user's ear in the scene. If you use positional audio, you have to move the listener with the player: For example, for a first-person player, you move the listener with the camera. For a third-person player, you move the listener with the player avatar Geometry.

  @Override
  public void simpleUpdate(float tpf) {
    // keep the audio listener moving with the camera
    listener.setLocation(cam.getLocation());
    listener.setRotation(cam.getRotation());
  }

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 setReverbEnabled(true) mentioned above.

EnvironmentdensitydiffusiongaingainHfdecayTimedecayHfreflGainreflDelaylateGainlateDelay
Garage 1.00f1.0f1.0f1.00f0.90f0.5f0.751f0.0039f0.661f0.0137f
Dungeon 0.75f1.0f1.0f0.75f1.60f1.0f0.950f0.0026f0.930f0.0103f
Cavern 0.50f1.0f1.0f0.50f2.25f1.0f0.908f0.0103f0.930f0.0410f
AcousticLab 0.50f1.0f1.0f1.00f0.28f1.0f0.870f0.0020f0.810f0.0080f
Closet 1.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. You can find many examples of audio environment presets here. 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:

sound, documentation, environment

view online version