您的位置:首页 > 编程语言

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

2011-08-12 11:31 375 查看
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());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: