简体   繁体   English

Java JToggleButton在While循环中冻结

[英]Java JToggleButton freezes in While loop

public void playMet() {

    int tempo = Integer.parseInt(met_speed.getText());
    tempo = tempo/60;
    int delay = tempo*1000; 

    if(Play.isSelected()) {
        try {
            FileInputStream in = new FileInputStream(new File("Click1.wav"));

            AudioStream as = new AudioStream(in);
            AudioPlayer.player.start(as);

            Thread.sleep(tempo*1000);
            playMet();

        } 
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    } 
    else
        System.out.println("not playing");
}

Here's a section of my code, basically when a button is pressed it plays a click X times per minute. 这是我的代码的一部分,基本上,当按下一个按钮时,它会每分钟播放一次X次点击。

I've tried the code with a while loop like this: 我已经尝试过使用while循环的代码了:

while(Play.isSelected()) {
.........
.........
}

and that didn't work either. 那也不起作用。 In both cases the program runs like it should, but freezes and I have to close it down from the task manager. 在这两种情况下,程序都可以正常运行,但是冻结了,我必须从任务管理器中关闭它。

How can I call a function when the button is pressed and be able to stop it if I deselect it? 按下按钮时如何调用功能,如果取消选择该功能就可以将其停止?

PS Just saw the two posts below. PS刚看到下面的两个帖子。 Have to go to work, will check up on them later and see if they work. 必须去上班,稍后再检查它们,看看它们是否起作用。 Thank you for the help. 感谢您的帮助。

The recursive calls are blocking the Event Dispatch Thread . 递归调用阻止了Event Dispatch Thread You have to create a new Thread to not block the EDT. 您必须创建一个新线程以不阻止EDT。

public void playMet()
{
    Thread t = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            while(Play.isSelected()){
                //Your code
            }
        }
    };
    t.start();
}

You don't need Thread.sleep(), you don't need recursion or while loop. 您不需要Thread.sleep(),不需要递归或while循环。 You need stateful player. 您需要有状态的球员。 And your UI should only change the state of the player. 而且您的用户界面仅应更改播放器的状态。

See an example in https://stackoverflow.com/a/7524627/2078908 请参阅https://stackoverflow.com/a/7524627/2078908中的示例

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> playingTask;
private FileInputStream playing;

public void playMet() {
    stopPlaying();
    playingTask = executor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            playMet();
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
}

public void playMetOnce(){
    try {
         IOUtils.close(playing);
         playing = new FileInputStream(new File("Click1.wav"));

         AudioStream as = new AudioStream(playing);
         AudioPlayer.player.start(as);
     } catch (Exception e)
     {
         JOptionPane.showMessageDialog(null, e);
     }
}

public void stopPlaying(){
    try {
        AudioPlayer.player.stop();
        IOUtils.close(playing);
        playing = null;
        if (playingTask != null) {
            playingTask.cancel(false);
        }
        playingTask = null;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

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

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