简体   繁体   English

Java jLayer应用程序在播放歌曲时冻结

[英]Java jLayer application freezes while playing song

I have a problem with jLayer. 我对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 :) 谢谢

It happens because the song-playing happens in the same thread as the GUI, or more specifically the EDT . 发生这种情况是因为歌曲的播放发生在与GUI或更具体地与EDT相同的线程中。 Therefore, the GUI does not response, when the song is playing. 因此,在播放歌曲时,GUI不响应。 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. 但是,您必须考虑到,由于GUI响应迅速,因此可以在歌曲仍在播放时开始播放歌曲。

For more information, see this 有关更多信息,请参阅

Tasks on the EDT (event dispatch thread) must finish quickly; EDT (事件分发线程)上的任务必须快速完成; if they don't, unhandled events back up and the user interface becomes unresponsive. 如果没有,则将备份未处理的事件,并且用户界面将变得无响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM