Nie wiem, jak zakończyć pracę playera w poniższym kodzie. Player nie reaguje na interrupt().
Korzystam z JLayera(w załaczniku).
KLASA 1:
package rnest.mp3.pak2;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Ramka extends JFrame {
private String filename;
private Thread watek;
public Ramka() throws FileNotFoundException, JavaLayerException{
filename = "D:\\mp3Java\\stripped.mp3";
System.out.println("Watek z konstr. ramki: " + Thread.currentThread());
setSize(300, 200);
JPanel panel = new JPanel();
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
panel.add(start);
panel.add(stop);
this.add(panel, BorderLayout.CENTER);
pack();
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
watek = new Thread(){
public void run(){
System.out.println("Watek z klaw. Start: " + Thread.currentThread());
while(!Thread.currentThread().isInterrupted()){
try {
new Player(new FileInputStream(filename)).play();
watek.wait();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
watek.start();
}});
stop.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new Thread(){
public void run(){
watek.interrupt();
System.out.println("Watek z klaw. Stop: " + Thread.currentThread());
}
}.start();
}});
}
}
KLASA 2:
package rnest.mp3.pak2;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
public class BodyMp3 {
public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
Ramka ramka = new Ramka();
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setVisible(true);
}
}
Ogólnie chodzi o to, że nie działa klawisz stop.