Advertisement
arthurassuncao

JLayerPausableTest

Jul 21st, 2012
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. /* *-----------------------------------------------------------------------
  2.  *   This program is free software; you can redistribute it and/or modify
  3.  *   it under the terms of the GNU Library General Public License as published
  4.  *   by the Free Software Foundation; either version 2 of the License, or
  5.  *   (at your option) any later version.
  6.  *
  7.  *   This program is distributed in the hope that it will be useful,
  8.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *   GNU Library General Public License for more details.
  11.  *
  12.  *   You should have received a copy of the GNU Library General Public
  13.  *   License along with this program; if not, write to the Free Software
  14.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  *  
  16.  *   Original by: http://thiscouldbebetter.wordpress.com/2011/07/04/pausing-an-mp3-file-using-jlayer/
  17.  *   Last modified: 21-jul-2012 by Arthur Assuncao
  18.  *----------------------------------------------------------------------
  19.  */
  20.  
  21. import java.awt.Dimension;
  22. import java.awt.GridBagLayout;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25.  
  26. import javax.swing.JButton;
  27. import javax.swing.JFrame;
  28. import javax.swing.JPanel;
  29.  
  30. import javazoom.jl.decoder.JavaLayerException;
  31.  
  32. //need JLayerPlayerPausable class
  33. public class JLayerPausableTest{
  34.     private SoundJLayer soundToPlay;
  35.  
  36.     public static void main(String[] args){
  37.         JLayerPausableTest jLayerPausable = new JLayerPausableTest();
  38.         jLayerPausable.soundToPlay = new SoundJLayer("temp.mp3");
  39.  
  40.         JLayerPausableTest.interfaceGrafica(jLayerPausable);
  41.     }
  42.  
  43.     //test only
  44.     public static void interfaceGrafica(JLayerPausableTest jLayerPausable){
  45.         JFrame frame = new JFrame("Test JlayerPlayerPausable");
  46.  
  47.         JButton buttonPlay = new JButton("Play");
  48.         JButton buttonPause = new JButton("Pause");
  49.         JButton buttonStop = new JButton("Stop");
  50.         JPanel panel = new JPanel(new GridBagLayout());
  51.  
  52.         panel.add(buttonPlay);
  53.         panel.add(buttonPause);
  54.         panel.add(buttonStop);
  55.  
  56.         buttonPlay.addActionListener(new ButtonListener(jLayerPausable));
  57.         buttonPause.addActionListener(new ButtonListener(jLayerPausable));
  58.         buttonStop.addActionListener(new ButtonListener(jLayerPausable));
  59.  
  60.         frame.add(panel);
  61.  
  62.         frame.setPreferredSize(new Dimension(200, 100));
  63.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         frame.pack();
  65.         frame.setLocationRelativeTo(null);
  66.  
  67.         frame.setVisible(true);
  68.     }
  69.  
  70.     public static class ButtonListener implements ActionListener{
  71.         JLayerPausableTest jLayerPausable;
  72.         public ButtonListener(JLayerPausableTest jLayerPausable){
  73.             this.jLayerPausable = jLayerPausable;
  74.         }
  75.         @Override
  76.         public void actionPerformed(ActionEvent evento) {
  77.             JButton botao = (JButton)evento.getSource();
  78.             if(botao.getText().equalsIgnoreCase("PLAY")){
  79.                 this.jLayerPausable.soundToPlay.play();
  80.             }
  81.             else if(botao.getText().equalsIgnoreCase("PAUSE")){
  82.                 this.jLayerPausable.soundToPlay.pauseToggle();
  83.             }
  84.             else if(botao.getText().equalsIgnoreCase("STOP")){
  85.                 this.jLayerPausable.soundToPlay.stop();
  86.             }
  87.         }
  88.     }
  89.  
  90. }
  91.  
  92. class SoundJLayer implements Runnable{
  93.     private String filePath;
  94.     private JLayerPlayerPausable player;
  95.     private Thread playerThread;
  96.     private String namePlayerThread = "AudioPlayerThread";
  97.     private PlaybackListener playbackListener = new PlaybackListener();
  98.  
  99.     public SoundJLayer(String filePath){
  100.         this.filePath = filePath;
  101.     }
  102.    
  103.     public SoundJLayer(String filePath, String namePlayerThread){
  104.         this.filePath = filePath;
  105.         this.namePlayerThread = namePlayerThread;
  106.     }
  107.  
  108.     public void play(){
  109.         if (this.player == null){
  110.             this.playerInitialize();
  111.         }
  112.         else if(!this.player.isPaused() || this.player.isComplete() || this.player.isStopped()){
  113.             this.stop();
  114.             this.playerInitialize();
  115.         }
  116.         this.playerThread = new Thread(this, namePlayerThread);
  117.         this.playerThread.setDaemon(true);
  118.  
  119.         this.playerThread.start();
  120.     }
  121.  
  122.     public void pause(){
  123.         if (this.player != null){
  124.             this.player.pause();
  125.  
  126.             if(this.playerThread != null){
  127.                 //this.playerThread.stop(); //unsafe method
  128.                 this.playerThread = null;
  129.             }
  130.         }
  131.     }
  132.  
  133.     public void pauseToggle(){
  134.         if (this.player != null){
  135.             if (this.player.isPaused() && !this.player.isStopped()){
  136.                 this.play();
  137.             }
  138.             else{
  139.                 this.pause();
  140.             }
  141.         }
  142.     }
  143.  
  144.     public void stop(){
  145.         if (this.player != null){
  146.             this.player.stop();
  147.  
  148.             if(this.playerThread != null){
  149.                 //this.playerThread.stop(); //unsafe method
  150.                 this.playerThread = null;
  151.             }
  152.         }
  153.     }
  154.  
  155.     /*private void playerInitialize(){
  156.         try{
  157.             String urlAsString =
  158.                     "file:///"
  159.                             + new java.io.File(".").getCanonicalPath()
  160.                             + "/"
  161.                             + this.filePath;
  162.  
  163.             this.player = new JLayerPlayerPausable(new java.net.URL(urlAsString));
  164.             this.player.setPlaybackListener(this.playbackListener);
  165.         }
  166.         catch (JavaLayerException e){
  167.             e.printStackTrace();
  168.         }
  169.     }*/
  170.     private void playerInitialize(){
  171.         try {
  172.             this.player = new JLayerPlayerPausable(this.filePath);
  173.             this.player.setPlaybackListener(this.playbackListener);
  174.         }
  175.         catch (JavaLayerException e) {
  176.             e.printStackTrace();
  177.         }
  178.     }
  179.  
  180.     // IRunnable members
  181.     public void run(){
  182.         try{
  183.             this.player.resume();
  184.         }
  185.         catch (javazoom.jl.decoder.JavaLayerException ex){
  186.             ex.printStackTrace();
  187.         }
  188.     }
  189.    
  190.     private static class PlaybackListener extends JLayerPlayerPausable.PlaybackAdapter {
  191.         // PlaybackListener members
  192.         @Override
  193.         public void playbackStarted(JLayerPlayerPausable.PlaybackEvent playbackEvent){
  194.             System.err.println("PlaybackStarted()");
  195.         }
  196.        
  197.         @Override
  198.         public void playbackPaused(JLayerPlayerPausable.PlaybackEvent playbackEvent){
  199.             System.err.println("PlaybackPaused()");
  200.         }
  201.  
  202.         @Override
  203.         public void playbackFinished(JLayerPlayerPausable.PlaybackEvent playbackEvent){
  204.             System.err.println("PlaybackStopped()");
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement