我创建了一个线程,该线程从实现的可运行类(原声带)中播放一首歌,我想按下我的按钮(jmenuitem1actionperformed)停止它。我搜索了它&尝试了多种停止方法,但失败了,我认为就我而言,还有另一种方法可以做到这一点。以下代码:
public static class Soundtrack implements Runnable {
@Override
public void run() {
try{
File file = new File("SF.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
try{
Player player = new Player(bis);
player.play();
}catch(JavaLayerException ex){}
}catch(IOException e){}
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
Thread background = new Thread(new Soundtrack());
background.start();
}
我想感谢姐姐的解决方案:
public static class Soundtrack implements Runnable {
@Override
public void run() {
try{
File file = new File("SF.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
try{
Player player = new Player(bis);
player.play();
}catch(JavaLayerException ex){}
}catch(IOException e){}
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
background.stop();
}
private static Thread background;
public static void main(String args[]) {
background = new Thread(new Soundtrack());
background.start();
}