gpt4 book ai didi

java - 打开AL空指针异常

转载 作者:行者123 更新时间:2023-12-03 02:09:58 25 4
gpt4 key购买 nike

我正在尝试使用开放式AL做我的第一个测试,并且不断收到空指针错误,如下所示:

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);

}

这就是我用来加载声音的东西。为了防止万一,我在下面链接了我的主类。让我知道您是否需要更多信息。

最佳答案

问题最终是我需要将缓冲区和源存储在int中,然后再将其推送通过。

通过执行以下操作解决

int缓冲区= AL10.alGenBuffers();
int源= AL10.alGenSources();

并使用它们代替.get(index)

关于java - 打开AL空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com