Player pessado

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]

Você poderia otimizar a parte while(true) que tem um if dentro e percorre alguns vetores. E rever aquele sleep(3000).

:thumbup:

Eu comentei esse if e o programa ficou mais leve… como posso fazer para continuar com essa opção mas de um jeito mais leve?

while(playing) { if(mp3.isStopped()) { //momento em que troca de música index++; label.setText(musicListener(index));

Para que serve está variável index que está sendo incrementada a cada iteração :?:

:thumbup:

É q cada vez que o player é uma lista de reprodução randômica então cada vez que inicia uma musica nova tem que pegar o nome da musica e jogar no JLabel. Esse index é parte dessa operação. Tem como fazer diferente?

Entendi. Aparentemente isto já está bem otimizado.

Pode ser partes do código acima. Vou tentar executá-lo…

:thumbup: