package robowebert;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class MP3Player{
private static MP3Player singleton;
private ExecutorService exe = Executors.newFixedThreadPool(1);
private File mp3;
private Player player;
private boolean tocando;
/**
* Método que inicia o programa
*
*/
public static void main(String[] fiofa){
new GUI();
}
/**
* Construtor privado sem argumentos
*/
private MP3Player(){}
/**
* Método que que retorna a instância única do kernel
*/
public static synchronized MP3Player getInstance(){
if(singleton == null) singleton = new MP3Player();
return singleton;
}
/**
* Método que altera o estado do player, se está tocando ou não.
*
* @param tocando
*/
public void setTocando(boolean tocando){
this.tocando = tocando;
}
/**
* Método que recebe o objeto File referenciando o arquivo
* MP3 a ser tocado e atribui ao atributo MP3 da classe.
*
* @param mp3
*/
public void setMP3(File mp3) {
this.mp3 = mp3;
if(!tocando) restartPlayer();
}
/**
* Método que toca o MP3
*/
public void play(){
if(!tocando) if(player != null) exe.execute(new Tocar(player));
}
/**
* Método que interrompe a execução do MP3
*/
public void stop(){
if(tocando) player.close();
}
/**
* Método que reinicia o player para que este possa
* tocar outro MP3
*/
public void restartPlayer(){
try{
player = new Player(new BufferedInputStream(new FileInputStream(mp3)));
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e){
e.printStackTrace();
}
}
private class Tocar implements Runnable{
/**
* Objeto Player da biblioteca jLayer. Ele tocará o arquivo
* MP3
*/
private Player player;
Tocar(Player p){
player = p;
}
public void run(){
System.gc();
try{
MP3Player kernel = MP3Player.getInstance();
kernel.setTocando(true);
player.play();
kernel.restartPlayer();
kernel.setTocando(false);
}catch(Exception e){
e.printStackTrace();
}
}
}
}