Im trying to play a wav file. the file is located in my resource folder which i used to load images and other game assets.
I first create a File and ensure the wav file exists.
I then use the WaveData utility to create WaveData.
The wave data utility return null every time.
I can use a String or FileInputStream when calling the create() and i get the same result
thoughts?
Please not this is test code not final

package com.iowind.mocha.sound;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.util.WaveData;
public class SoundUtil {
private IntBuffer buffer = BufferUtils.createIntBuffer(1); // hold sound data
private IntBuffer source = BufferUtils.createIntBuffer(1); // sources of sounds
FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // postion of sound source
FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // velocity of sound
FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // position of listener
FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // velocity of listener
FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f });/** Orientation of the listener. (first 3 elements are "at", second 3 are "up") */
public void play(){
try{
String wavFile = "resources/sounds/music/DreamRaidPartI.wav";
File file = new File(wavFile);
if(file.exists()){
System.out.println("File Exists");
//WaveData waveData = WaveData.create(wavFile);
WaveData waveData = WaveData.create(new FileInputStream(file));
System.out.println("WaveData = "+ waveData);
AL.create();
if(AL10.alGetError() == AL10.AL_NO_ERROR){
System.out.println("No Errors");
}
AL10.alGenBuffers(buffer);
AL10.alBufferData(buffer.get(0), waveData.format, waveData.data, waveData.samplerate);
waveData.dispose();
AL10.alGenSources(source);
AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer.get(0) );
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.alListener(AL10.AL_POSITION, listenerPos);
AL10.alListener(AL10.AL_VELOCITY, listenerVel);
AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
AL10.alSourcePlay(source.get(0));
}
}catch(LWJGLException | IOException e){
e.printStackTrace();
}
}
@Override
protected void finalize() throws Throwable {
AL.destroy();
}
public static void main(String[] args) {
SoundUtil util = new SoundUtil();
util.play();
}
}
The output of this code is:
File Exists
WaveData = null
No Errors
Exception in thread "main" java.lang.NullPointerException
at com.iowind.mocha.sound.SoundUtil.play(SoundUtil.java:43)
at com.iowind.game.apollo.MusicTest.main(MusicTest.java:9)
AL lib: (EE) alc_cleanup: 1 device not closed