Home | History | Annotate | Download | only in helloworld
      1 package jme3test.helloworld;
      2 
      3 import com.jme3.app.SimpleApplication;
      4 import com.jme3.audio.AudioNode;
      5 import com.jme3.input.controls.ActionListener;
      6 import com.jme3.input.controls.MouseButtonTrigger;
      7 import com.jme3.material.Material;
      8 import com.jme3.math.ColorRGBA;
      9 import com.jme3.math.Vector3f;
     10 import com.jme3.scene.Geometry;
     11 import com.jme3.scene.shape.Box;
     12 
     13 /** Sample 11 - playing 3D audio. */
     14 public class HelloAudio extends SimpleApplication {
     15 
     16   private AudioNode audio_gun;
     17   private AudioNode audio_nature;
     18   private Geometry player;
     19 
     20   public static void main(String[] args) {
     21     HelloAudio app = new HelloAudio();
     22     app.start();
     23   }
     24 
     25   @Override
     26   public void simpleInitApp() {
     27     flyCam.setMoveSpeed(40);
     28 
     29     /** just a blue box floating in space */
     30     Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
     31     player = new Geometry("Player", box1);
     32     Material mat1 = new Material(assetManager,
     33             "Common/MatDefs/Misc/Unshaded.j3md");
     34     mat1.setColor("Color", ColorRGBA.Blue);
     35     player.setMaterial(mat1);
     36     rootNode.attachChild(player);
     37 
     38     /** custom init methods, see below */
     39     initKeys();
     40     initAudio();
     41   }
     42 
     43   /** We create two audio nodes. */
     44   private void initAudio() {
     45     /* gun shot sound is to be triggered by a mouse click. */
     46     audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
     47     audio_gun.setLooping(false);
     48     audio_gun.setVolume(2);
     49     rootNode.attachChild(audio_gun);
     50 
     51     /* nature sound - keeps playing in a loop. */
     52     audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", true);
     53     audio_nature.setLooping(true);  // activate continuous playing
     54     audio_nature.setPositional(true);
     55     audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
     56     audio_nature.setVolume(3);
     57     rootNode.attachChild(audio_nature);
     58     audio_nature.play(); // play continuously!
     59   }
     60 
     61   /** Declaring "Shoot" action, mapping it to a trigger (mouse click). */
     62   private void initKeys() {
     63     inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
     64     inputManager.addListener(actionListener, "Shoot");
     65   }
     66 
     67   /** Defining the "Shoot" action: Play a gun sound. */
     68   private ActionListener actionListener = new ActionListener() {
     69     @Override
     70     public void onAction(String name, boolean keyPressed, float tpf) {
     71       if (name.equals("Shoot") && !keyPressed) {
     72         audio_gun.playInstance(); // play each instance once!
     73       }
     74     }
     75   };
     76 
     77   /** Move the listener with the a camera - for 3D audio. */
     78   @Override
     79   public void simpleUpdate(float tpf) {
     80     listener.setLocation(cam.getLocation());
     81     listener.setRotation(cam.getRotation());
     82   }
     83 
     84 }
     85