Pessoal estou com um player que eu fiz em java mas está ocupando todo o desempenho da cpu, gostaria de saber oque posso fazer para ele rodar mais light e não ficar pesando tanto?
[code]import jaco.mp3.player.MP3Player;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Tocador extends JPanel{
private static final File[] listMusic = new File("musica").listFiles(); //poem a pasta das musicas
private static final File[] listComercial = new File("comercial").listFiles(); //poem a pasta dos comerciais
private Thread control;
private MP3Player mp3;
private String t1,t2,t3;
private JLabel label;
private JButton leave;
private int index;
public Tocador()
{
super(new BorderLayout());
label = new JLabel();
leave = new JButton("Sair");
leave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int i = JOptionPane.showConfirmDialog(null ,"Deseja fechar a aplicação?",
"Saída",JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION ) {
System.exit(0);
}
}
});
this.add(label, BorderLayout.NORTH);
this.add(leave, BorderLayout.CENTER);
}
public void play()
{
Random r = new Random();
while(true)
{
if(control == null)
{
final File file1 = listMusic[r.nextInt(listMusic.length)];
final File file2 = listMusic[r.nextInt(listMusic.length)];
final File file3 = listComercial[r.nextInt(listComercial.length)];
t1 = file1.getName();
t2 = file2.getName();
t3 = file3.getName();
control = new Thread(new Runnable() {
@Override
public void run() {
mp3 = new MP3Player(file1,file2,file3);
mp3.play();
mp3IsStillPlaying();
}
});
control.start();
}
else
{
if(!control.isAlive())
{
control.interrupt();
control = null;
}
}
}
}
private String musicListener(int op)
{
switch(op)
{
case 1:
return t1;
case 2:
return t2;
case 3:
return t3;
default:
return "";
}
}
private void mp3IsStillPlaying()
{
boolean playing = true;
index = 1;
label.setText(musicListener(index));
while(playing)
{
if(mp3.isStopped())
{
//momento em que troca de música
index++;
label.setText(musicListener(index));
try {
Thread.sleep(3000);
if(mp3.isStopped())
{
playing = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
[/code]