OpenAL加载两个声音冻结Java

时间:2014-07-30 02:57:27

标签: java lwjgl audio openal

所以我目前正在使用OpenAL来加载声音,但是当我在我的库中加载多个声音时,它会冻结所有内容。这是当前与加载声音相关的代码。

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 buffers.get(count).intValue();
            }
        }

        int buffer = loadALBuffer(path);

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

        return buffer;
    }


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

主类代码:

static int testSound;
static int testSoun;


//to add a sound call manager.loadALSample("path",is this looping) and add the variable. 
    public static void loadALData(SoundManager manager) throws FileNotFoundException {

        //test = manager.loadALSample("test.wav", false);
        testSound = SoundManager.loadALSample("res/sounds/test1.wav", false);
        testSoun= SoundManager.loadALSample("res/sounds/test2.wav", false);
        SoundManager.killALLoadedData();
    }

如果您需要更多信息,请与我们联系。这很难调试,我有错误检查,第一个声音加载好,然后当第二个声音加载时它就冻结了。

1 个答案:

答案 0 :(得分:0)

这是因为我在加载每个样本后没有清除加载的文件,这导致Java在尝试加载数据时锁定。