LWJGL Forum

Programming => OpenAL => Topic started by: Cottonwood on December 18, 2010, 00:12:27

Title: [solved] lwjgl Slick Sound; How can I check whether a sound is playing or not?
Post by: Cottonwood on December 18, 2010, 00:12:27
The thread startet here: http://lwjgl.org/forum/index.php/topic,3629.0.html
But though the subject changed I'm beginning a new one.

I've reduced the sample from the old thread to the minimum to play just one song:
import java.io.FileInputStream;
import java.io.IOException;

import org.lwjgl.openal.AL;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;

public class SoundExample {
   /** The ogg sound effect */
   private static Audio oggEffect;

   /**
    * Main method
    */
   public static void main(String[] argv) {
       //        soundExample.start();
       try {oggEffect = AudioLoader.getAudio("OGG", new FileInputStream
               ("testdata/The Pretenders - Angel Of The Morning.ogg"));
       } catch (IOException e) {e.printStackTrace();}
       oggEffect.playAsSoundEffect(1.0f, 1.0f, true);

       System.out.println("begin to sleep");        
       try{Thread.sleep(200000);
       } catch (InterruptedException e) {e.printStackTrace();}
       System.out.println("sleep ended");        
       AL.destroy();
   }
}


As you can see I have a sleep to prevent the program from stopping before the sound ends. That method doesn't really make sense.
With native AL it should be like this:
   public static void main(String[] argv) {
       //        soundExample.start();
       try {oggEffect = AudioLoader.getAudio("OGG", new FileInputStream
               ("testdata/The Pretenders - Angel Of The Morning.ogg"));
       } catch (IOException e) {e.printStackTrace();}
       oggEffect.playAsSoundEffect(1.0f, 1.0f, true);
       while (AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING){
           try{Thread.sleep(100);
           } catch (InterruptedException e) {e.printStackTrace();}
       }


But I cannot solve the source for the AL10.alGetSourcei.

What can I do here? I can't find something like that at AudioLoader here: http://slick.cokeandcode.com/javadoc-util/

//Edit: I think this could help: http://slick.cokeandcode.com/javadoc/org/newdawn/slick/openal/AudioImpl.html
But I don't know how to implement the isPlaying(). ???
Title: Re: lwjgl Slick Sound; How can I check whether a sound is playing or not?
Post by: jediTofu on December 18, 2010, 03:40:18
if(oggEffect.isPlaying()) {
  //...
}

http://slick.cokeandcode.com/javadoc-util/org/newdawn/slick/openal/Audio.html#isPlaying() (http://slick.cokeandcode.com/javadoc-util/org/newdawn/slick/openal/Audio.html#isPlaying())
Title: Re: lwjgl Slick Sound; How can I check whether a sound is playing or not?
Post by: Cottonwood on December 18, 2010, 07:26:00
Thank you. I was thinking much too complicated. :-X

Now it works fine.
import java.io.FileInputStream;
import java.io.IOException;

import org.lwjgl.openal.AL;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;

public class SoundExample {
    /** The ogg sound effect */
    private static Audio oggEffect;

    /**
     * Main method
     */
    public static void main(String[] argv) {
        //        soundExample.start();
        try {oggEffect = AudioLoader.getAudio("OGG", new FileInputStream
                ("testdata/The Pretenders - Angel Of The Morning.ogg"));
        } catch (IOException e) {e.printStackTrace();}
        oggEffect.playAsMusic(1.0f, 1.0f, false);
        while (oggEffect.isPlaying()){
            try{Thread.sleep(100);
            } catch (InterruptedException e) {e.printStackTrace();}
        }
        AL.destroy();
    }
}


But even this works perfectly. Who's able to read has definitely an edge over others. :'(

import java.io.FileInputStream;
import java.io.IOException;

import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;

public class SoundExample {
    /** The ogg sound effect */
    private static Audio oggEffect;

    /**
     * Main method
     */
    public static void main(String[] argv) {
        //        soundExample.start();
        try {oggEffect = AudioLoader.getAudio("OGG", new FileInputStream
                ("testdata/The Pretenders - Angel Of The Morning.ogg"));
        } catch (IOException e) {e.printStackTrace();}
        int source=oggEffect.playAsSoundEffect(1.0f, 1.0f, false);
        while (AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING){
            try{Thread.sleep(100);
            } catch (InterruptedException e) {e.printStackTrace();}
        }
        AL.destroy();
    }
}