LWJGL
May 21, 2013, 10:10:43 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: [solved] lwjgl Slick Sound; How can I check whether a sound is playing or not?  (Read 1933 times)
Cottonwood
Regular nerd
**
Posts: 50



WWW
« on: December 17, 2010, 16: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:
Code:
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:
   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(). Huh
Logged

Regards. Cottonwood.
jediTofu
Prolific Timewaster
****
Posts: 280



« Reply #1 on: December 17, 2010, 19:40:18 »

if(oggEffect.isPlaying()) {
  //...
}

http://slick.cokeandcode.com/javadoc-util/org/newdawn/slick/openal/Audio.html#isPlaying()
Logged

cool story, bro
Cottonwood
Regular nerd
**
Posts: 50



WWW
« Reply #2 on: December 17, 2010, 23:26:00 »

Thank you. I was thinking much too complicated. Lips sealed

Now it works fine.
Code:
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. Cry

Code:
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();
    }
}
Logged

Regards. Cottonwood.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!