打开AL Null指针异常

时间:2014-07-27 04:20:13

标签: java audio nullpointerexception lwjgl openal

我试图用开放A​​L进行我的第一次测试,并且我一直得到一个空指针错误,如下所示:

Exception in thread "main"
C:\Users\Rob\Desktop\Programming Workspace\Android\RPG Test Engine\res\sounds\test1.wav
java.lang.NullPointerException
at com.evylgaming.rpg.SoundManager.getSoundsFromFile(SoundManager.java:119)
at com.evylgaming.rpg.RPGMain.main(RPGMain.java:14)
AL lib: (EE) alc_cleanup: 1 device not closed

您看到的链接是指错误的位置。我想确保文件已加载,所以我做错了什么?

package com.evylgaming.rpg;

import java.io.*;
import java.nio.FloatBuffer;
import java.util.HashMap;

import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.newdawn.slick.openal.WaveData;

import javax.sound.sampled.*;

public class SoundManager {

    private File soundsFolder;
    private File[] soundFolderFiles;

    static HashMap<String, Integer> buffers = new HashMap<>();
    static HashMap<String, Integer> sources = new HashMap<>();
    static HashMap<String, float[]> sourcePositions = new HashMap<>();
    static HashMap<String, float[]> sourceVelocitys = new HashMap<>();
    static                 float[]  listenerPosition;
    static                 float[]  listenerVelocity;

    // static HashMap<String, Point> locations = new Hashmap<String, Point>();
    // AL_BUFFER, AL_POSITION, AL_PITCH, AL_GAIN

    public void playSound(int index) {
        AL10.alSourcePlay(sources.get(index));
    }

    public void stopSound(int index) {
        AL10.alSourceStop(sources.get(index));
    }

    public void pauseSound(int index) {
        AL10.alSourcePause(sources.get(index));
    }

    public void playSound(String name) {

        if(sources.containsKey(name)) {
            AL10.alSourcePlay(sources.get(name));
        } else {}

    }

    public void stopSound(String name) {
        if(sources.containsKey(name)) {
            AL10.alSourceStop(sources.get(name));
        } else {}

    }

    public void pauseSound(String name) {
        if(sources.containsKey(name)) {
            AL10.alSourcePause(sources.get(name));
        } else {}
    }

    public void setSourcePosition(int index, float x, float y, float z) {
        AL10.alSource3f(sources.get(index), AL10.AL_POSITION, x, y, z);
        System.out.println("Source at index: " + index + " set to position X:" + x + " Y:" + y + " Z: " + z);
    }
    public void setSourceVelocity(int index, float x, float y, float z) {
        AL10.alSource3f(sources.get(index), AL10.AL_VELOCITY, x, y, z);
        System.out.println("Source at index: " + index + " set to Velocity X:" + x + " Y:" + y + " Z: " + z);
    }

    public SoundManager(String fileName) {
        soundsFolder = new File(fileName);
        soundFolderFiles = soundsFolder.listFiles();
    }

    // Init for open AL
    public void getSoundsFromFile(String fileName) throws FileNotFoundException, IOException {
        int index = 0;
        try {
            AL.create();
        } catch (LWJGLException e) {
            System.out.println("Open AL failed to initialize");
            e.printStackTrace();
        }
        for (int i = 0; i < soundFolderFiles.length; i++) {
            if( !buffers.containsKey(soundFolderFiles[index])) {
                WaveData soundData;

                soundData = WaveData.create(new BufferedInputStream(new FileInputStream(soundFolderFiles[index].getCanonicalPath())));

                System.out.println(soundFolderFiles[index].getCanonicalPath());
                buffers.put(soundFolderFiles[index].getCanonicalPath(), AL10.alGenBuffers());
                AL10.alBufferData(buffers.get(index), soundData.format, soundData.data, soundData.samplerate);
                sources.put(soundFolderFiles[index].getCanonicalPath(), AL10.alGenSources());

                sourcePositions.put(soundFolderFiles[index].getCanonicalPath(), new float[ ] {0, 0, 0});
                sourceVelocitys.put(soundFolderFiles[index].getCanonicalPath(), new float[ ] {0, 0, 0});

                AL10.alSourcei(sources.get(index), AL10.AL_BUFFER,buffers.get(index));
                AL10.alSource3f(sources.get(index), AL10.AL_POSITION, 0, 0,  0);
                AL10.alSource3f(sources.get(index), AL10.AL_VELOCITY, 0, 0, 0);

                soundData.dispose();
            }
            index += 1;

        }
        // Load listener
        AL10.alListener3f(AL10.AL_POSITION, 0, 0, 0);
        AL10.alListener3f(AL10.AL_VELOCITY, 0, 0, 0);

    }

这就是我用来加载声音的方法。我已将下面的主要课程链接起来以防万一。如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

问题最终是我需要在推送之前将缓冲区和源存储在int中。

通过执行以下操作解决

int buffer = AL10.alGenBuffers(); int source = AL10.alGenSources();

用这些代替.get(索引)