首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开AL空指针异常

打开AL空指针异常
EN

Stack Overflow用户
提问于 2014-07-27 04:20:13
回答 1查看 360关注 0票数 0

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

代码语言:javascript
复制
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

您所看到的链接是针对错误所在的。我想确保文件被加载了,所以我是不是做错了什么?

代码语言:javascript
复制
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);

    }

这就是我用来装载声音的东西。为了以防万一,我已经把我下面的主班联系起来了。如果你需要更多的信息,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-27 07:08:28

问题的最终是,我需要将缓冲区和源存储在int中,然后才能完成它。

通过执行以下操作解决

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

并使用这些来代替.get(索引)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24977882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档