Advertisement
kajacx

Java play mp3

Jun 30th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. package audiosurf;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import javazoom.jl.decoder.JavaLayerException;
  8. import javazoom.jl.player.*;
  9. import javazoom.jl.player.advanced.*;
  10.  
  11. /**
  12.  *
  13.  * @author Karel Hrkal
  14.  */
  15. public class Test {
  16.  
  17.     private static ThreadPlayer tp;
  18.  
  19.     public static void main(String[] args) throws Exception {
  20.  
  21.  
  22.         tp = new ThreadPlayer("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3");
  23.         new Thread(tp).start();
  24.        
  25.        
  26.         JFrame main = new JFrame("Test");
  27.         main.setLayout(new FlowLayout());
  28.  
  29.         JButton start = new JButton("Start");
  30.         start.addActionListener(new StartListener());
  31.         main.add(start);
  32.  
  33.         JButton stop = new JButton("Stop");
  34.         stop.addActionListener(new StopListener());
  35.         main.add(stop);
  36.  
  37.         main.pack();
  38.         main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         main.setVisible(true);
  40.        
  41.         AdvancedPlayer ap;
  42.        
  43.         ap = new AdvancedPlayer(new FileInputStream("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3"));
  44.         ap.play(200);
  45.        
  46.        
  47.         /*Toolkit.getDefaultToolkit().beep();
  48.        
  49.         ap = new AdvancedPlayer(new FileInputStream("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3"));
  50.         ap.play(500, 1000);*/
  51.        
  52.        
  53.     }
  54.  
  55.     private static void start() {
  56.         if (!tp.isPlaying()) {
  57.             tp.pause(true);
  58.         }
  59.     }
  60.  
  61.     private static void stop() {
  62.         if (tp.isPlaying()) {
  63.             tp.pause(false);
  64.         }
  65.     }
  66.  
  67.     private static class StartListener implements ActionListener {
  68.  
  69.         public void actionPerformed(ActionEvent e) {
  70.             start();
  71.         }
  72.     }
  73.  
  74.     private static class StopListener implements ActionListener {
  75.  
  76.         public void actionPerformed(ActionEvent e) {
  77.             stop();
  78.         }
  79.     }
  80.  
  81.     private static class ThreadPlayer implements Runnable {
  82.  
  83.         private String fileName;
  84.         private int position;
  85.         private boolean isPlaying;
  86.         private boolean die;
  87.         private AdvancedPlayer ap;
  88.  
  89.         public ThreadPlayer(String fileName) {
  90.             this.fileName = fileName;
  91.         }
  92.  
  93.         public void run() {
  94.             while (true) {
  95.                 if (isPlaying) {
  96.                     try {
  97.                         ap = new AdvancedPlayer(new FileInputStream(fileName));
  98.                         ap.setPlayBackListener(new MyPlaybackListener());
  99.                         ap.play(position, Integer.MAX_VALUE);
  100.                     } catch (FileNotFoundException ex) {
  101.                         System.out.println("File " + fileName + " was not found!");
  102.                         ex.printStackTrace(System.out);
  103.                     } catch (JavaLayerException ex) {
  104.                         ex.printStackTrace(System.out);
  105.                     }
  106.                 }
  107.                 try {
  108.                     Thread.sleep(1);
  109.                 } catch (InterruptedException ex) {
  110.                 }
  111.                 if (die) {
  112.                     break;
  113.                 }
  114.             }
  115.         }
  116.        
  117.         /**
  118.          * Starts to play the music file from its begin.
  119.          */
  120.  
  121.         public void start() {
  122.             position = 0;
  123.             isPlaying = true;
  124.         }
  125.        
  126.         /**
  127.          * Stops playing the music file. <br/>
  128.          * Warning: do NOT call this function before the start() function.
  129.          */
  130.  
  131.         public void stop() {
  132.             position = 0;
  133.             isPlaying = false;
  134.             ap.stop();
  135.         }
  136.  
  137.         /**
  138.          * Pauses / unpauses playing music file, depending on isPlaying value. <br/>
  139.          * Warning: do NOT call this function before the start() function.
  140.          */
  141.         public void pause() {
  142.             pause(!isPlaying);
  143.         }
  144.  
  145.         /**
  146.          * Pauses / unpauses playing music file, depending on play value. <br/>
  147.          * Noting will happen when both play and isPlaying is true, and vise versa. <br/>
  148.          * Warning: do NOT call this function before the start() function.
  149.          * @param play - true if the song hat to conctine its playing, or false if the song has to be paused.
  150.          */
  151.         public void pause(boolean play) {
  152.             if (play) {
  153.                 isPlaying = true;
  154.             } else {
  155.                 isPlaying = false;
  156.                 ap.stop();
  157.             }
  158.         }
  159.        
  160.         /**
  161.          * Closes the player, making it to stop making any sound and stopping the run() function. <br/>
  162.          * Warning: do NOT call this function before the start() function.
  163.          */
  164.  
  165.         public void close() {
  166.             ap.close();
  167.             isPlaying = false;
  168.             die = true;
  169.         }
  170.  
  171.         public boolean isPlaying() {
  172.             return isPlaying;
  173.         }
  174.  
  175.         private class MyPlaybackListener extends PlaybackListener {
  176.  
  177.             public void playbackFinished(PlaybackEvent evt) {
  178.                 position = evt.getFrame();
  179.                 System.out.println("position on : " + position);
  180.             }
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement