Hello Guest

[solved] lwjgl Slick Sound; How can I check whether a sound is playing or not?

  • 2 Replies
  • 11646 Views
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:
Code: [Select]
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:
Code: [Select]
   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(). ???
« Last Edit: December 18, 2010, 07:26:41 by Cottonwood »
Regards. Cottonwood.


Re: lwjgl Slick Sound; How can I check whether a sound is playing or not?
« Reply #2 on: December 18, 2010, 07:26:00 »
Thank you. I was thinking much too complicated. :-X

Now it works fine.
Code: [Select]
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. :'(

Code: [Select]
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();
    }
}
« Last Edit: December 18, 2010, 07:47:02 by Cottonwood »
Regards. Cottonwood.