如何结束该线程?

时间:2018-07-19 16:07:53

标签: java

我正在Eclipse Java中工作,我做了以下方法来从名为“音乐”的文件夹中播放音乐...

public static void effect(String fileName, boolean loop) throws InterruptedException {
        Thread effect = new Thread() {
            public void run() {
                Mixer.Info[] mixInfos = AudioSystem.getMixerInfo();
                mixer = AudioSystem.getMixer(mixInfos[0]);

                DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
                try {
                    clip = (Clip) mixer.getLine(dataInfo);
                } catch (LineUnavailableException lue) {
                    lue.printStackTrace();
                }

                try {
                    URL soundURL = Main.class.getResource("/music/" + fileName + ".wav");
                    AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
                    clip.open(audioStream);
                } catch (LineUnavailableException lue) {
                    lue.printStackTrace();
                } catch (UnsupportedAudioFileException uafe) {
                    uafe.printStackTrace();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
                clip.start();
                do {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                } while (clip.isActive());
                if (clip.isActive() == false && loop == true) {
                    play(fileName, loop);
                }
            }
        };
        effect.start();
        effect.join();
    }

如何通过此方法运行一个可以更改以停止音乐的变量?例如,我将更改play = false,并且将停止播放任何音乐。谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

不需要额外的变量,只需在要暂停音乐时调用clip.stop(),然后clip.start()即可在音乐停止处恢复播放。

来自DataLine.stop()'s javadoc

  

停止行。停止的线路应停止I / O活动。但是,如果该生产线是开路并正在运行,则它应保留恢复活动所需的资源。

答案 1 :(得分:0)

执行此操作,而不要像将其变成void

public static Thread effect(String fileName, boolean loop) throws InterruptedException {
        Thread effect = new Thread() {
            public void run() {
                Mixer.Info[] mixInfos = AudioSystem.getMixerInfo();
                mixer = AudioSystem.getMixer(mixInfos[0]);

                DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
                try {
                    clip = (Clip) mixer.getLine(dataInfo);
                } catch (LineUnavailableException lue) {
                    lue.printStackTrace();
                }

                try {
                    URL soundURL = Main.class.getResource("/music/" + fileName + ".wav");
                    AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
                    clip.open(audioStream);
                } catch (LineUnavailableException lue) {
                    lue.printStackTrace();
                } catch (UnsupportedAudioFileException uafe) {
                    uafe.printStackTrace();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
                clip.start();
                do {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                        return;
                    }
                } while (clip.isActive());
                if (clip.isActive() == false && loop == true) {
                    play(fileName, loop);
                }
            }
        };
        effect.start();
        effect.join();

        return effect;
    }

只要您想停止它,只需执行以下操作:

//Declare and start working the thread
Thread thread = effect(params...);
//Whenever you want stop it
thread.interrupt();