Java OpenAL:按键时播放声音,但仅在短时间内播放

Java OpenAL:按键时播放声音,但仅在短时间内播放,java,audio,input,lwjgl,openal,Java,Audio,Input,Lwjgl,Openal,最后,我得到了第一个要播放的声音,但是当我按下按键时,它会播放一毫秒的声音,当我释放它时,它会继续播放其余的声音。下面是分别调用的声音类和输入法。这与我如何检查输入有关吗 package com.evylgaming.rpg; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.nio.FloatBuffer;

最后,我得到了第一个要播放的声音,但是当我按下按键时,它会播放一毫秒的声音,当我释放它时,它会继续播放其余的声音。下面是分别调用的声音类和输入法。这与我如何检查输入有关吗

package com.evylgaming.rpg;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Iterator;

import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.ALC10;
import org.lwjgl.openal.OpenALException;
import org.newdawn.slick.openal.WaveData;

public class SoundManager {
    public static final int NUM_BUFFERS = 3;
    public static final int NUM_SOURCES = 3;


    private static ArrayList<Integer> sources = new ArrayList<Integer>();
    private static ArrayList<Integer> buffers = new ArrayList<Integer>();
    private static ArrayList<String> loadedFiles = new ArrayList<String>();

    static FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3*NUM_BUFFERS);


    static FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3*NUM_BUFFERS);

    FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(
            new float[] { 0.0f, 0.0f, 0.0f });
    FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(
            new float[] { 0.0f, 0.0f, 0.0f });
    FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(
            new float[] { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f });



    void setListenerValues() {
        AL10.alListener(AL10.AL_POSITION, listenerPos);
        AL10.alListener(AL10.AL_VELOCITY, listenerVel);
        AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
    }

    public static void killALData() {
        IntBuffer scratch = BufferUtils.createIntBuffer(1);

        // Release all buffer data.
        for (Iterator<Integer> iter = buffers.iterator(); iter.hasNext();) {
            scratch.put(0, ((Integer) iter.next()).intValue());
            AL10.alDeleteBuffers(scratch);
        }

        // Release all source data.
        for (Iterator<Integer> iter = sources.iterator(); iter.hasNext();) {
            scratch.put(0, ((Integer) iter.next()).intValue());
            AL10.alDeleteSources(scratch);
        }

        // Destroy the lists.
        buffers.clear();
        sources.clear();
    }

    public SoundManager(String fileName) {
        listenerPos.flip();
        listenerVel.flip();
        listenerOri.flip();
    }

    public static String getALErrorString(int err) {
        switch (err) {
        case AL10.AL_NO_ERROR:
            return "AL_NO_ERROR";
        case AL10.AL_INVALID_NAME:
            return "AL_INVALID_NAME";
        case AL10.AL_INVALID_ENUM:
            return "AL_INVALID_ENUM";
        case AL10.AL_INVALID_VALUE:
            return "AL_INVALID_VALUE";
        case AL10.AL_INVALID_OPERATION:
            return "AL_INVALID_OPERATION";
        case AL10.AL_OUT_OF_MEMORY:
            return "AL_OUT_OF_MEMORY";
        default:
            return "No such error code";
        }
    }

    public static String getALCErrorString(int err) {
        switch (err) {
        case ALC10.ALC_NO_ERROR:
            return "AL_NO_ERROR";
        case ALC10.ALC_INVALID_DEVICE:
            return "ALC_INVALID_DEVICE";
        case ALC10.ALC_INVALID_CONTEXT:
            return "ALC_INVALID_CONTEXT";
        case ALC10.ALC_INVALID_ENUM:
            return "ALC_INVALID_ENUM";
        case ALC10.ALC_INVALID_VALUE:
            return "ALC_INVALID_VALUE";
        case ALC10.ALC_OUT_OF_MEMORY:
            return "ALC_OUT_OF_MEMORY";
        default:
            return "no such error code";
        }
    }

    public static int loadALBuffer(String path) throws FileNotFoundException {
        int result;
        IntBuffer buffer = BufferUtils.createIntBuffer(1);

        // Load wav data into a buffers.
        AL10.alGenBuffers(buffer);

        if ((result = AL10.alGetError()) != AL10.AL_NO_ERROR) {
            throw new OpenALException(getALErrorString(result));
        }

        WaveData waveFile = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
        if (waveFile != null) {
            AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data,
                    waveFile.samplerate);
            waveFile.dispose();
        } else {
            throw new RuntimeException("No such file: " + path);
        }

        // Do another error check and return.
        if ((result = AL10.alGetError()) != AL10.AL_NO_ERROR) {
            throw new OpenALException(getALErrorString(result));
        }

        return buffer.get(0);
    }

    public static int getLoadedALBuffer(String path) throws FileNotFoundException {
        int count = 0;
        for (Iterator<String> i = loadedFiles.iterator(); i.hasNext(); count++) {
            if (i.equals(path)) {
                return ((Integer) buffers.get(count)).intValue();
            }
        }

        int buffer = loadALBuffer(path);

        buffers.add(new Integer(buffer));
        loadedFiles.add(path);

        return buffer;
    }

    public static void killALLoadedData() {
        loadedFiles.clear();
    }

    public static void playSound(int index){
        AL10.alSourcePlay(index);
    }
    public static void stopSound(int index){
        AL10.alSourceStop(index);
    }


    public static int loadALSample(String path, boolean loop) throws FileNotFoundException {
        IntBuffer source = BufferUtils.createIntBuffer(1);
        int buffer;
        int result;


        buffer = getLoadedALBuffer(path);
        AL10.alGenSources(source);

        if ((result = AL10.alGetError()) != AL10.AL_NO_ERROR)
            throw new OpenALException(getALErrorString(result));

        AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer);
        AL10.alSourcef(source.get(0), AL10.AL_PITCH, 1.0f);
        AL10.alSourcef(source.get(0), AL10.AL_GAIN, 1.0f);
        AL10.alSource(source.get(0), AL10.AL_POSITION, sourcePos);
        AL10.alSource(source.get(0), AL10.AL_VELOCITY, sourceVel);
        AL10.alSourcei(source.get(0), AL10.AL_LOOPING, (loop ? AL10.AL_TRUE: AL10.AL_FALSE));

        sources.add(new Integer(source.get(0)));

        return source.get(0);
    }
}  
这里是处理一切的输入类

package com.evylgaming.rpg;

import org.lwjgl.input.Keyboard;

public class RPGInputMap {
    public static int CONTROL_CODE_NUM = 5;
    public static int LEFT = 0;
    public static int RIGHT = 1;
    public static int UP = 2;
    public static int DOWN = 3;
    public static int MENU = 4;

    private boolean[] controlDelta;
    private boolean[] controlState;
    private int[] controlBinding;

    public RPGInputMap() {
        controlDelta = new boolean[5];
        controlState = new boolean[5];
        controlBinding = new int[5];
        controlBinding[0] = Keyboard.KEY_A;
        controlBinding[1] = Keyboard.KEY_D;
        controlBinding[2] = Keyboard.KEY_W;
        controlBinding[3] = Keyboard.KEY_S;
        controlBinding[4] = Keyboard.KEY_ESCAPE;
    }
    public boolean getRawDelta(int controlCode) {
        if(controlCode >= CONTROL_CODE_NUM) {
            return false;
        }
        return controlDelta[controlCode];
    }
    public boolean getRawState(int controlCode) {
        if(controlCode >= CONTROL_CODE_NUM) {
            return false;
        }
        return controlState[controlCode];
    }
    public void pollControls() {
        for(int i=0; i <controlDelta.length; i++) {
            controlDelta[i]=false;
        }
        while (Keyboard.next()) {
            for(int i=0; i<controlBinding.length; i++) {
                if(controlBinding[i]==Keyboard.getEventKey()) {
                    controlDelta[i]=true;
                    controlState[i]=Keyboard.getEventKeyState();
                }
            }
        }
    }
}
package com.evylgaming.rpg;
导入org.lwjgl.input.Keyboard;
公共类RPGInputMap{
公共静态整数控制\代码\数量=5;
公共静态int LEFT=0;
公共静态int-RIGHT=1;
公共静态int UP=2;
公共静态int-DOWN=3;
公共静态int菜单=4;
私有布尔[]控制增量;
私有布尔[]控制状态;
私有int[]控件绑定;
公共RPGInputMap(){
controlDelta=新布尔值[5];
controlState=新布尔值[5];
controlBinding=newint[5];
controlBinding[0]=Keyboard.KEY\u A;
controlBinding[1]=Keyboard.KEY\u D;
controlBinding[2]=Keyboard.KEY\u W;
controlBinding[3]=Keyboard.KEY\S;
controlBinding[4]=Keyboard.KEY\u ESCAPE;
}
公共布尔getRawDelta(int-controlCode){
如果(控制代码>=控制代码数量){
返回false;
}
返回controlDelta[controlCode];
}
公共布尔getRawState(int-controlCode){
如果(控制代码>=控制代码数量){
返回false;
}
返回控制状态[控制代码];
}
公共控制(){

对于(inti=0;i,我发现这是由于输入类是如何实现的

package com.evylgaming.rpg;

import org.lwjgl.input.Keyboard;

public class RPGInputMap {
    public static int CONTROL_CODE_NUM = 5;
    public static int LEFT = 0;
    public static int RIGHT = 1;
    public static int UP = 2;
    public static int DOWN = 3;
    public static int MENU = 4;

    private boolean[] controlDelta;
    private boolean[] controlState;
    private int[] controlBinding;

    public RPGInputMap() {
        controlDelta = new boolean[5];
        controlState = new boolean[5];
        controlBinding = new int[5];
        controlBinding[0] = Keyboard.KEY_A;
        controlBinding[1] = Keyboard.KEY_D;
        controlBinding[2] = Keyboard.KEY_W;
        controlBinding[3] = Keyboard.KEY_S;
        controlBinding[4] = Keyboard.KEY_ESCAPE;
    }
    public boolean getRawDelta(int controlCode) {
        if(controlCode >= CONTROL_CODE_NUM) {
            return false;
        }
        return controlDelta[controlCode];
    }
    public boolean getRawState(int controlCode) {
        if(controlCode >= CONTROL_CODE_NUM) {
            return false;
        }
        return controlState[controlCode];
    }
    public void pollControls() {
        for(int i=0; i <controlDelta.length; i++) {
            controlDelta[i]=false;
        }
        while (Keyboard.next()) {
            for(int i=0; i<controlBinding.length; i++) {
                if(controlBinding[i]==Keyboard.getEventKey()) {
                    controlDelta[i]=true;
                    controlState[i]=Keyboard.getEventKeyState();
                }
            }
        }
    }
}