Pregunta

I have a problem with jLayer. I have a button with code for starting a song:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {        
    try {
        Player prehravac;
        FileInputStream buff = new FileInputStream(Okno.filename);
        prehravac = new Player(buff);
        prehravac.play();
        if (prehravac != null) {
            prehravac.close();
            this.dispose();
        }
    } catch(Exception e) {

    }
}                                    

When I click on this button, it begins to play the song but whole application freezes and I cannot click on anything. When the song ends, it's okay and I can click on other components again.

Can someone help me pls ? :) thank

¿Fue útil?

Solución

It happens because the song-playing happens in the same thread as the GUI, or more specifically the EDT. Therefore, the GUI does not response, when the song is playing. To fix this, do something like this:

new Thread(){
  run(){
    //Your play code
  }
}.start();

This will play your stuff in a seperate Thread. However, you have to regard that since the GUI is responsive, you can start songs while songs are still playing.

For more information, see this

Otros consejos

Tasks on the EDT (event dispatch thread) must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top