Domanda

I am trying to make my game play two different pieces of music one after another. I am using two separate clips because each one of them is associated to a different animation. In order to make the second music clip wait for the first one to finish I am using Timer.schedule(Task aTask, float delay). This seems to work fine. However, after the second music clip finishes I need to change the screen and dispose of the current screen, so I’m using Timer.schedule(Task aTask, float delay) again so that the change happens after the second music clip finishes. However, I am having problems with placing dispose() inside the Timer.Task as the console gives an error starting:

Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: buffer not allocated with newUnsafeByteBuffer or already disposed …

My code looks like this:

        aMusic.play();

        Timer.schedule(new Task(){
        @Override
        public void run(){
            anotherMusic.play();
        }
        },2.0f);
        Timer.schedule(new Task(){
        @Override
        public void run(){              
            aGame.setScreen(new newScreen(aGame));
            dispose();              
        }
        },10.0f);

Is there another way to make the dispose() method wait other than using Timer (and while keeping the thread active as the audio needs to be playing)? Thanks ccm

È stato utile?

Soluzione

You can use an OnCompleteListener for this:

music.setOnCompletionListener(new OnCompletionListener() {

     @Override
     public void onCompletion(Music music) {

     }
});

This way you can wait until the first track is completed, play the second and dispose it... :)

Hope it helps...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top