Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* *-----------------------------------------------------------------------
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Library General Public License as published
- * by the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * Original by: http://thiscouldbebetter.wordpress.com/2011/07/04/pausing-an-mp3-file-using-jlayer/
- * Last modified: 21-jul-2012 by Arthur Assuncao
- *----------------------------------------------------------------------
- */
- import java.awt.Dimension;
- import java.awt.GridBagLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javazoom.jl.decoder.JavaLayerException;
- //need JLayerPlayerPausable class
- public class JLayerPausableTest{
- private SoundJLayer soundToPlay;
- public static void main(String[] args){
- JLayerPausableTest jLayerPausable = new JLayerPausableTest();
- jLayerPausable.soundToPlay = new SoundJLayer("temp.mp3");
- JLayerPausableTest.interfaceGrafica(jLayerPausable);
- }
- //test only
- public static void interfaceGrafica(JLayerPausableTest jLayerPausable){
- JFrame frame = new JFrame("Test JlayerPlayerPausable");
- JButton buttonPlay = new JButton("Play");
- JButton buttonPause = new JButton("Pause");
- JButton buttonStop = new JButton("Stop");
- JPanel panel = new JPanel(new GridBagLayout());
- panel.add(buttonPlay);
- panel.add(buttonPause);
- panel.add(buttonStop);
- buttonPlay.addActionListener(new ButtonListener(jLayerPausable));
- buttonPause.addActionListener(new ButtonListener(jLayerPausable));
- buttonStop.addActionListener(new ButtonListener(jLayerPausable));
- frame.add(panel);
- frame.setPreferredSize(new Dimension(200, 100));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public static class ButtonListener implements ActionListener{
- JLayerPausableTest jLayerPausable;
- public ButtonListener(JLayerPausableTest jLayerPausable){
- this.jLayerPausable = jLayerPausable;
- }
- @Override
- public void actionPerformed(ActionEvent evento) {
- JButton botao = (JButton)evento.getSource();
- if(botao.getText().equalsIgnoreCase("PLAY")){
- this.jLayerPausable.soundToPlay.play();
- }
- else if(botao.getText().equalsIgnoreCase("PAUSE")){
- this.jLayerPausable.soundToPlay.pauseToggle();
- }
- else if(botao.getText().equalsIgnoreCase("STOP")){
- this.jLayerPausable.soundToPlay.stop();
- }
- }
- }
- }
- class SoundJLayer implements Runnable{
- private String filePath;
- private JLayerPlayerPausable player;
- private Thread playerThread;
- private String namePlayerThread = "AudioPlayerThread";
- private PlaybackListener playbackListener = new PlaybackListener();
- public SoundJLayer(String filePath){
- this.filePath = filePath;
- }
- public SoundJLayer(String filePath, String namePlayerThread){
- this.filePath = filePath;
- this.namePlayerThread = namePlayerThread;
- }
- public void play(){
- if (this.player == null){
- this.playerInitialize();
- }
- else if(!this.player.isPaused() || this.player.isComplete() || this.player.isStopped()){
- this.stop();
- this.playerInitialize();
- }
- this.playerThread = new Thread(this, namePlayerThread);
- this.playerThread.setDaemon(true);
- this.playerThread.start();
- }
- public void pause(){
- if (this.player != null){
- this.player.pause();
- if(this.playerThread != null){
- //this.playerThread.stop(); //unsafe method
- this.playerThread = null;
- }
- }
- }
- public void pauseToggle(){
- if (this.player != null){
- if (this.player.isPaused() && !this.player.isStopped()){
- this.play();
- }
- else{
- this.pause();
- }
- }
- }
- public void stop(){
- if (this.player != null){
- this.player.stop();
- if(this.playerThread != null){
- //this.playerThread.stop(); //unsafe method
- this.playerThread = null;
- }
- }
- }
- /*private void playerInitialize(){
- try{
- String urlAsString =
- "file:///"
- + new java.io.File(".").getCanonicalPath()
- + "/"
- + this.filePath;
- this.player = new JLayerPlayerPausable(new java.net.URL(urlAsString));
- this.player.setPlaybackListener(this.playbackListener);
- }
- catch (JavaLayerException e){
- e.printStackTrace();
- }
- }*/
- private void playerInitialize(){
- try {
- this.player = new JLayerPlayerPausable(this.filePath);
- this.player.setPlaybackListener(this.playbackListener);
- }
- catch (JavaLayerException e) {
- e.printStackTrace();
- }
- }
- // IRunnable members
- public void run(){
- try{
- this.player.resume();
- }
- catch (javazoom.jl.decoder.JavaLayerException ex){
- ex.printStackTrace();
- }
- }
- private static class PlaybackListener extends JLayerPlayerPausable.PlaybackAdapter {
- // PlaybackListener members
- @Override
- public void playbackStarted(JLayerPlayerPausable.PlaybackEvent playbackEvent){
- System.err.println("PlaybackStarted()");
- }
- @Override
- public void playbackPaused(JLayerPlayerPausable.PlaybackEvent playbackEvent){
- System.err.println("PlaybackPaused()");
- }
- @Override
- public void playbackFinished(JLayerPlayerPausable.PlaybackEvent playbackEvent){
- System.err.println("PlaybackStopped()");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement