JME基础教程代码分析11 音频

package com.hello;

import com.jme3.app.SimpleApplication;

import com.jme3.audio.AudioNode;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

 

/** Sample 11 - playing 3D audio. */

public class HelloAudio extends SimpleApplication {

 //声音节点

  private AudioNode audio_gun;

  private AudioNode audio_nature;

  private Geometry player;

 

  public static void main(String[] args) {

    HelloAudio app = new HelloAudio();

    app.start();

  }

 

  @Override

  public void simpleInitApp() {

    flyCam.setMoveSpeed(40);

 

    /** just a blue box floating in space */

    Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);

    player = new Geometry("Player", box1);

    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat1.setColor("Color", ColorRGBA.Blue);

    player.setMaterial(mat1);

    rootNode.attachChild(player);

 

    /** custom init methods, see below */

    initKeys();

    initAudio();

  }

 

  /** We create two audio nodes. */

  private void initAudio() {

    /* gun shot sound is to be triggered by a mouse click. */

  //加载声音文件,不循环

    audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);

    audio_gun.setLooping(false);

    // Volume goes from 0.1f to 1.0f. At 0 it's muted.

    audio_gun.setVolume(0.2f);

 

    /* nature sound - keeps playing in a loop. */

    //循环,开始启动

    audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false);

    audio_nature.setLooping(true);

    audio_nature.setPositional(true);

    //3d效果中声音有远近的分别

    audio_nature.setLocalTranslation(Vector3f.ZERO.clone());

    // Volume goes from 0.1f to 1.0f. At 0 it's muted.

    audio_nature.setVolume(0.3f);

    //持续播放

    audioRenderer.playSource(audio_nature); // play continuously!

  }

 

  /** Declaring the "Shoot" action, and

   *  mapping it to a trigger (mouse click). */

  private void initKeys() {

    inputManager.addMapping("Shoot", new MouseButtonTrigger(0));

    inputManager.addListener(actionListener, "Shoot");

  }

 

  /** Defining the "Shoot" action: Play a gun sound. */

  private ActionListener actionListener = new ActionListener() {

    @Override

    public void onAction(String name, boolean keyPressed, float tpf) {

      if (name.equals("Shoot") && !keyPressed) {

        audioRenderer.playSource(audio_gun); // play once!

      }

    }

  };

 

  /** Move the listener with the a camera - for 3D audio. */

  @Override

  public void simpleUpdate(float tpf) {

    listener.setLocation(cam.getLocation());

    listener.setRotation(cam.getRotation());

  }

 

}

转载于:https://www.cnblogs.com/suhuan/archive/2011/08/12/2135931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值