Advertisement
Guest User

MP3Player.java

a guest
Sep 4th, 2011
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package robowebert;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9.  
  10. import javazoom.jl.decoder.JavaLayerException;
  11. import javazoom.jl.player.Player;
  12.  
  13. public class MP3Player{
  14.     private static MP3Player singleton;
  15.     private ExecutorService exe = Executors.newFixedThreadPool(1);
  16.     private File mp3;
  17.     private Player player;
  18.     private boolean tocando;
  19.  
  20.     /**
  21.      * Método que inicia o programa
  22.      *
  23.      */
  24.     public static void main(String[] fiofa){
  25.         new GUI();
  26.     }
  27.     /**
  28.      * Construtor privado sem argumentos
  29.      */
  30.     private MP3Player(){}
  31.     /**
  32.      * Método que que retorna a instância única do kernel
  33.      */
  34.     public static synchronized MP3Player getInstance(){
  35.         if(singleton == null) singleton = new MP3Player();
  36.         return singleton;
  37.     }
  38.     /**
  39.      * Método que altera o estado do player, se está tocando ou não.
  40.      *
  41.      * @param tocando
  42.      */
  43.     public void setTocando(boolean tocando){
  44.         this.tocando = tocando;
  45.     }
  46.     /**
  47.      * Método que recebe o objeto File referenciando o arquivo
  48.      * MP3 a ser tocado e atribui ao atributo MP3 da classe.
  49.      *
  50.      * @param mp3
  51.      */
  52.     public void setMP3(File mp3) {
  53.         this.mp3 = mp3;
  54.         if(!tocando) restartPlayer();
  55.     }
  56.     /**
  57.      * Método que toca o MP3
  58.      */
  59.     public void play(){
  60.         if(!tocando) if(player != null) exe.execute(new Tocar(player));
  61.     }
  62.     /**
  63.      * Método que interrompe a execução do MP3
  64.      */
  65.     public void stop(){
  66.         if(tocando) player.close();
  67.     }
  68.     /**
  69.      * Método que reinicia o player para que este possa
  70.      * tocar outro MP3
  71.      */
  72.     public void restartPlayer(){
  73.         try{
  74.             player = new Player(new BufferedInputStream(new FileInputStream(mp3)));
  75.         }catch(FileNotFoundException e){
  76.             e.printStackTrace();
  77.         }catch(JavaLayerException e){
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.     private class Tocar implements Runnable{
  82.         /**
  83.          * Objeto Player da biblioteca jLayer. Ele tocará o arquivo
  84.          * MP3
  85.          */
  86.         private Player player;
  87.        
  88.         Tocar(Player p){
  89.             player = p;
  90.         }
  91.         public void run(){
  92.             System.gc();
  93.             try{
  94.                 MP3Player kernel = MP3Player.getInstance();
  95.                 kernel.setTocando(true);
  96.                         player.play();
  97.                         kernel.restartPlayer();
  98.                         kernel.setTocando(false);
  99.                 }catch(Exception e){
  100.                         e.printStackTrace();
  101.                 }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement