Advertisement
Guest User

Untitled

a guest
May 26th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. package fr.rinaorc.jltest;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5.  
  6. import javazoom.jl.decoder.JavaLayerException;
  7. import javazoom.jl.player.AudioDevice;
  8. import javazoom.jl.player.Player;
  9.  
  10. public class PausablePlayer {
  11.  
  12.     private final static int NOTSTARTED = 0;
  13.     private final static int PLAYING = 1;
  14.     private final static int PAUSED = 2;
  15.     private final static int FINISHED = 3;
  16.  
  17.     // the player actually doing all the work
  18.     private final Player player;
  19.  
  20.     // locking object used to communicate with player thread
  21.     private final Object playerLock = new Object();
  22.  
  23.     // status variable what player thread is doing/supposed to do
  24.     private int playerStatus = NOTSTARTED;
  25.  
  26.     public PausablePlayer(final InputStream inputStream) throws JavaLayerException {
  27.         this.player = new Player(inputStream);
  28.     }
  29.  
  30.     public PausablePlayer(final InputStream inputStream, final AudioDevice audioDevice) throws JavaLayerException {
  31.         this.player = new Player(inputStream, audioDevice);
  32.     }
  33.  
  34.     /**
  35.      * Starts playback (resumes if paused)
  36.      */
  37.     public void play() throws JavaLayerException {
  38.         synchronized (playerLock) {
  39.             switch (playerStatus) {
  40.                 case NOTSTARTED:
  41.                     final Runnable r = new Runnable() {
  42.                         public void run() {
  43.                             playInternal();
  44.                         }
  45.                     };
  46.                     final Thread t = new Thread(r);
  47.                     t.setDaemon(true);
  48.                     t.setPriority(Thread.MAX_PRIORITY);
  49.                     playerStatus = PLAYING;
  50.                     t.start();
  51.                     break;
  52.                 case PAUSED:
  53.                     resume();
  54.                     break;
  55.                 default:
  56.                     break;
  57.             }
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Pauses playback. Returns true if new state is PAUSED.
  63.      */
  64.     public boolean pause() {
  65.         synchronized (playerLock) {
  66.             if (playerStatus == PLAYING) {
  67.                 playerStatus = PAUSED;
  68.             }
  69.             return playerStatus == PAUSED;
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Resumes playback. Returns true if the new state is PLAYING.
  75.      */
  76.     public boolean resume() {
  77.         synchronized (playerLock) {
  78.             if (playerStatus == PAUSED) {
  79.                 playerStatus = PLAYING;
  80.                 playerLock.notifyAll();
  81.             }
  82.             return playerStatus == PLAYING;
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Stops playback. If not playing, does nothing
  88.      */
  89.     public void stop() {
  90.         synchronized (playerLock) {
  91.             playerStatus = FINISHED;
  92.             playerLock.notifyAll();
  93.         }
  94.     }
  95.  
  96.     private void playInternal() {
  97.         while (playerStatus != FINISHED) {
  98.             try {
  99.                 if (!player.play(1)) {
  100.                     break;
  101.                 }
  102.             } catch (final JavaLayerException e) {
  103.                 break;
  104.             }
  105.             // check if paused or terminated
  106.             synchronized (playerLock) {
  107.                 while (playerStatus == PAUSED) {
  108.                     try {
  109.                         playerLock.wait();
  110.                     } catch (final InterruptedException e) {
  111.                         // terminate player
  112.                         break;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         close();
  118.     }
  119.  
  120.     /**
  121.      * Closes the player, regardless of current state.
  122.      */
  123.     public void close() {
  124.         synchronized (playerLock) {
  125.             playerStatus = FINISHED;
  126.         }
  127.         try {
  128.             player.close();
  129.         } catch (final Exception e) {
  130.             // ignore, we are terminating anyway
  131.         }
  132.     }
  133.  
  134.     // demo how to use
  135.     public static void main(String[] argv) {
  136.         try {
  137.             FileInputStream input = new FileInputStream("myfile.mp3");
  138.             PausablePlayer player = new PausablePlayer(input);
  139.  
  140.             // start playing
  141.             player.play();
  142.  
  143.             // after 5 secs, pause
  144.             Thread.sleep(5000);
  145.             player.pause();    
  146.  
  147.             // after 5 secs, resume
  148.             Thread.sleep(5000);
  149.             player.resume();
  150.         } catch (final Exception e) {
  151.             throw new RuntimeException(e);
  152.         }
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement