Hello Guest

Problem with WaveData - NullPointer

  • 9 Replies
  • 25086 Views
Problem with WaveData - NullPointer
« on: March 22, 2011, 16:42:41 »
Hello,

Today i've started to do some things with OpenAL and i want to load a Wave-File, but if i try to load one with the WaveData Class (org.lwjgl.util.WaveData) the returned WaveData Object is a NullPointer.

Here's my code:
Code: [Select]
package com.iceengine.audio.sound;

/**
 *
 * @author penguin
 */
import org.lwjgl.util.WaveData;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.AL;

import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
public class WaveSound implements Sound {
    private WaveData myData;
    private IntBuffer alBuffer = BufferUtils.createIntBuffer(1);
    private IntBuffer alSource = BufferUtils.createIntBuffer(1);

    public void setListener(float[] Position, float[] Velocity, float[] Orientation)
    {
        AL10.alListener3f(AL10.AL_POSITION, Position[0], Position[1], Position[2]);
        AL10.alListener3f(AL10.AL_VELOCITY, Velocity[0], Velocity[1], Velocity[2]);
        AL10.alListener3f(AL10.AL_ORIENTATION, Orientation[0], Orientation[1], Orientation[2]);
    }
    public void loadSound(String Path)
    {
        myData = WaveData.create(Path);
        AL10.alGenBuffers(alBuffer);
        System.out.println(alBuffer + " " + myData);
        System.out.println(alBuffer + " " + myData.format + " " + myData.data + " " + myData.samplerate);
        AL10.alBufferData(alBuffer.get(0), myData.format, myData.data, myData.samplerate);
        myData.dispose();
    }
    public void genSources(float[] sourcePos, float[] sourceVelocity)
    {
        AL10.alGenSources(alSource);
        AL10.alSourcei(alSource.get(0), AL10.AL_BUFFER, alBuffer.get(0));
        AL10.alSourcef(alSource.get(0), AL10.AL_PITCH, 1.0f);
        AL10.alSourcef(alSource.get(0), AL10.AL_GAIN, 1.0f);
        AL10.alSource3f(alSource.get(0), AL10.AL_POSITION, sourcePos[0], sourcePos[1], sourcePos[2]);
        AL10.alSource3f(alSource.get(0), AL10.AL_VELOCITY, sourceVelocity[0], sourceVelocity[1], sourceVelocity[2]);
    }

    public void killSource()
    {
        AL10.alDeleteBuffers(alBuffer.get(0));
        AL10.alDeleteSources(alSource.get(0));
    }

    public void init()
    {
        try {
            AL.create();
        } catch (Exception e)
        {

        }
    }
    public void startPlay()
    {
        AL10.alSourcePlay(alSource.get(0));
    }
    public void stopPlay()
    {
        AL10.alSourcePause(alSource.get(0));
    }
    public void haltPlay()
    {
        AL10.alSourceStop(alSource.get(0));
    }
}

Here's the code how i use my class:

Code: [Select]
          WaveSound mySound = new WaveSound();
          mySound.init();
          float[] tmp = {0.0f, 0.0f, 0.0f};
          mySound.setListener(tmp, tmp, tmp);
          mySound.loadSound("hell.wav"); // hell.wav exists in Project folder, also tried with absolute path, but don't work.
          mySound.genSources(tmp, tmp);
          mySound.startPlay();

I hope someone can help me and, btw sorry for my bad english (I'm German).

Regards,
Penguin

*

Offline kappa

  • *****
  • 1319
Re: Problem with WaveData - NullPointer
« Reply #1 on: March 22, 2011, 19:34:24 »
That usually just means that its not finding the sound files, try putting your sound files in a jar file and adding it to the classpath.

Re: Problem with WaveData - NullPointer
« Reply #2 on: March 25, 2011, 20:40:04 »
Sorry for late answer, i was very busy.
This works, but is there no other choice to play the Soundfile?

Re: Problem with WaveData - NullPointer
« Reply #3 on: March 26, 2011, 00:29:03 »
Code: [Select]
import java.io.FileInputStream;
//...
    FileInputStream fin = null;
    WaveData waveFile = null;
    try {
      fin = new FileInputStream("MyFile.wav");
      waveFile = WaveData.create(fin);
    } catch (java.io.FileNotFoundException ex) {
      ex.printStackTrace();
    }
    finally {
      if(fin != null) {
        try{ fin.close(); }catch(java.io.IOException ex){}
      }
    }

LWJGL Tutorials
« Last Edit: March 26, 2011, 00:30:37 by jediTofu »
cool story, bro

Re: Problem with WaveData - NullPointer
« Reply #4 on: October 06, 2011, 22:34:55 »
Sorry, to dig this out, but I just had a similiar problem when trying to load a wav from the file system and doubt that the example code is wrong. Whatever I tried, I always just got a null pointer exception after trying to load the data. Actually when loading it from a file system and the target file does not exist, the FileNotFoundException will be raised already. However, the file was found, but I still got just a null back. After going down a little bit into the code, I noticed, that it is using an own logger that was actually preventing to show the real problem. I am actually not sure, if it is a got idea to wrap an exception into a hidden logger, but... After adding
Code: [Select]
System.setProperty("org.lwjgl.util.Debug", "true");i see the following message:
"Unable to create from inputstream, mark/reset not supported"

This actually gave me the right hint. I think that it is completely right as FileInputStream should not support mark/reset, so I wrapped it into a BufferedStream:
Code: [Select]
wavefile = WaveData.create(new BufferedInputStream(fin));And suddenly the sound is loaded correctly. However, I am not an expert with streams and maybe someone can validate, if the current listing for loading sound files from the filesystem is correct. Else there might be a lot of newcomers having problems to just play a sound.

Re: Problem with WaveData - NullPointer
« Reply #5 on: November 30, 2011, 21:03:58 »
Thanks, I had the exact same problem.

I would like to use the WaveData.create(String path) but it seems to have some bugs because it loads the wave in eclipse but it wont load it when I pack my project to jar and run it in windows (is debug mode the reason ? you might wanna check that guys). And my jar project was working in windows properly except the sounds. So this is what I use now and it works good.
Code: [Select]
WaveData wavefile = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
// btw: Thanks for the bug fix in 2.8.2 with the exit crash!
« Last Edit: November 30, 2011, 21:07:23 by n4pgamer »

Re: Problem with WaveData - NullPointer
« Reply #6 on: February 26, 2012, 06:00:17 »
n4pgamer :

That's interesting.  I'm having the same issue.  If I use your example
Code: [Select]
WaveData wavefile = WaveData.create(new BufferedInputStream(new FileInputStream(path)));

That works if my asset path is outside my Jar file.  However, I'd love to be able to package my assets (sounds and images) inside the jar.

I was trying to get my sound file this way and it works great in Eclipse, but as n4pgamer mentioned, it breaks when packaged.

Code: [Select]
    ClassLoader load =  this.getClass().getClassLoader();
    WaveData waveFile = WaveData.create(load.getResourceAsStream("assets/scene_01/sounds/spacebackground.wav"));


That works great for other types of files.  For example, this code works great for getting image files from the jar
Code: [Select]
    ClassLoader load = this.getClass().getClassLoader();
    URL url = load.getResource(fileName);
    image = ImageIO.read(url);

Has anyone found a way to get sound files into WaveData when the sound files are packaged in the Jar?

Thank you

Greg

Re: Problem with WaveData - NullPointer
« Reply #7 on: February 26, 2012, 06:14:51 »
I think I found it

Code: [Select]
    ClassLoader load =  this.getClass().getClassLoader();
    String path = "assets/scene_01/sounds/spacebackground.wav";
    URL url = load.getResource(path);
    WaveData waveFile = WaveData.create(url);


That seems to work with the asset directory in the jar file

Re: Problem with WaveData - NullPointer
« Reply #8 on: September 23, 2012, 09:49:07 »

Code: [Select]
wavefile = WaveData.create(new BufferedInputStream(fin));

Thanks alot!

I was unable to load the file from my jar. It worked in Eclipse. Simply wrapping a BufferedInputStream around the FileInputStream fixed the loading problem of sound files.

Re: Problem with WaveData - NullPointer
« Reply #9 on: November 02, 2013, 22:55:35 »
wrapping it in a bufferedInputstream didnt work for me.. but loading it into a ByteArrayInputStream and then doing .create did work.

wonder if the size of the file had an impact?