Skip to content

Commit

Permalink
seems find one mp3 player that works
Browse files Browse the repository at this point in the history
  • Loading branch information
gtxistxgao committed Sep 8, 2016
1 parent 8641889 commit 1cf9676
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Logger/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="C:/Program Files/JLayer1.0.1/jl1.0.1.jar"/>
<classpathentry kind="lib" path="/Users/frankgao/Downloads/JLayer1.0.1/jl1.0.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added Logger/bin/Logger/LoggerClass$1.class
Binary file not shown.
Binary file added Logger/bin/Logger/LoggerClass.class
Binary file not shown.
Binary file modified Logger/bin/Logger/MusicPlayer.class
Binary file not shown.
Binary file modified Logger/bin/MultiThreadingLearning/MP3Player.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Logger/bin/SwingStudyExample/MusicPlayTest.class
Binary file not shown.
155 changes: 155 additions & 0 deletions Logger/src/MultiThreadingLearning/PausablePlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package MultiThreadingLearning;

import java.io.FileInputStream;
import java.io.InputStream;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.Player;

public class PausablePlayer {

private final static int NOTSTARTED = 0;
private final static int PLAYING = 1;
private final static int PAUSED = 2;
private final static int FINISHED = 3;

// the player actually doing all the work
private final Player player;

// locking object used to communicate with player thread
private final Object playerLock = new Object();

// status variable what player thread is doing/supposed to do
private int playerStatus = NOTSTARTED;

public PausablePlayer(final InputStream inputStream) throws JavaLayerException {
this.player = new Player(inputStream);
}

public PausablePlayer(final InputStream inputStream, final AudioDevice audioDevice) throws JavaLayerException {
this.player = new Player(inputStream, audioDevice);
}

/**
* Starts playback (resumes if paused)
*/
public void play() throws JavaLayerException {
synchronized (playerLock) {
switch (playerStatus) {
case NOTSTARTED:
final Runnable r = new Runnable() {
public void run() {
playInternal();
}
};
final Thread t = new Thread(r);
t.setDaemon(true);
t.setPriority(Thread.MAX_PRIORITY);
playerStatus = PLAYING;
t.start();
break;
case PAUSED:
resume();
break;
default:
break;
}
}
}

/**
* Pauses playback. Returns true if new state is PAUSED.
*/
public boolean pause() {
synchronized (playerLock) {
if (playerStatus == PLAYING) {
playerStatus = PAUSED;
}
return playerStatus == PAUSED;
}
}

/**
* Resumes playback. Returns true if the new state is PLAYING.
*/
public boolean resume() {
synchronized (playerLock) {
if (playerStatus == PAUSED) {
playerStatus = PLAYING;
playerLock.notifyAll();
}
return playerStatus == PLAYING;
}
}

/**
* Stops playback. If not playing, does nothing
*/
public void stop() {
synchronized (playerLock) {
playerStatus = FINISHED;
playerLock.notifyAll();
}
}

private void playInternal() {
while (playerStatus != FINISHED) {
try {
if (!player.play(1)) {
break;
}
} catch (final JavaLayerException e) {
break;
}
// check if paused or terminated
synchronized (playerLock) {
while (playerStatus == PAUSED) {
try {
playerLock.wait();
} catch (final InterruptedException e) {
// terminate player
break;
}
}
}
}
close();
}

/**
* Closes the player, regardless of current state.
*/
public void close() {
synchronized (playerLock) {
playerStatus = FINISHED;
}
try {
player.close();
} catch (final Exception e) {
// ignore, we are terminating anyway
}
}

// demo how to use
public static void main(String[] argv) {
try {
FileInputStream input = new FileInputStream("myfile.mp3");
PausablePlayer player = new PausablePlayer(input);

// start playing
player.play();

// after 5 secs, pause
Thread.sleep(5000);
player.pause();

// after 5 secs, resume
Thread.sleep(5000);
player.resume();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 1cf9676

Please sign in to comment.