Hello Guest

Sound not playing

  • 2 Replies
  • 9812 Views
Sound not playing
« on: November 02, 2013, 20:18:30 »
I'm pretty new to OpenAL and I can't really get my head around it, so you will probably find the answer pretty obvious :P I would also be really thankful if you could explain a little bit more on in which order to do things and why it doesn't work ;)

Here's my code:
Code: [Select]
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.IntBuffer;
import java.util.ArrayList;

import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL;
import org.lwjgl.util.WaveData;

import static org.lwjgl.openal.AL10.*;

public class AudioManager{

private Main M;
private IntBuffer buffer = BufferUtils.createIntBuffer(1);
private IntBuffer source = BufferUtils.createIntBuffer(128);
private ArrayList<WaveData> waveFiles = new ArrayList<WaveData>();

public AudioManager(Main M){
this.M=M;
try{
AL.create();
}catch(Exception e){
e.printStackTrace();
}
initializeAudio();
}

private void initializeAudio(){
alGenBuffers(buffer);


try{
waveFiles.add(WaveData.create(new BufferedInputStream(new FileInputStream("res/audio/music.wav"))));
}catch(Exception e){e.printStackTrace();}

for(int i=0;i<waveFiles.size();i++){
alBufferData(buffer.get(i), waveFiles.get(i).format, waveFiles.get(i).data, waveFiles.get(i).samplerate);
waveFiles.get(i).dispose();
}
}

public void playSound(int i, boolean b, float x, float y, float z){
alGenSources(source);

alSourcei(source.get(i), AL_BUFFER, buffer.get(i));
alSourcef(source.get(i), AL_PITCH, 1);
alSourcef(source.get(i), AL_GAIN, 3);
alSource3f(source.get(i), AL_POSITION, x, y, z);
alSource3f(source.get(i), AL_VELOCITY, 0, 0, 0);

if(b)alSourcei(source.get(i), AL_LOOPING, AL_TRUE);
else alSourcei(source.get(i), AL_LOOPING, AL_FALSE);

alSourcePlay(source.get(i));
}

public void setListener(){
alListener3f(AL_POSITION, M.C.getX(), M.C.getY(), M.C.getZ());
alListener3f(AL_VELOCITY, 0, 0, 0);
alListener3f(AL_ORIENTATION, 0, 0, 0);
}

}

And here is my game loop:
Code: [Select]
public void gameLoop(){
AM.setListener();
AM.playSound(0, true, C.getX(), C.getY(), C.getZ());
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
AM.setListener();
C.draw3D();
glLoadIdentity();
C.useView();

R.render();
IM.update();

C.draw2D();
glLoadIdentity();
R.render2D();

Display.update();
Display.sync(70);
}
}
The problem is that I can't hear the sound :/ And I'm sure that I've done something really stupid :P
If you wonder what M.C.getX(); getY() and getZ() does it's basicly returning the player's position, or to be more exact, the listening point.

Thanks in advance! :)

Re: Sound not playing
« Reply #1 on: November 02, 2013, 22:40:51 »
UPDATE: I've noticed that for some reason, the specific wav file I'm trying to play simply won't play!  I tried with another file and it worked! The weird thing is that I can play both of them in Windows Media Player, but the one sound that I wanna play, can't be played with OpenAL! Are there any specific requirements for the wav file?

Re: Sound not playing
« Reply #2 on: November 02, 2013, 22:53:46 »
Hi  SporreKing

I just created a sound playing utility and had a few trip ups too..

make sure that you .rewind all of your buffers before they get used (include the one that contains your music)

let me know if that fixes the issue.

j.