Skip to content

Commit

Permalink
improving CPlayer API
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Nov 10, 2023
1 parent 9e95c34 commit f210ea4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
39 changes: 18 additions & 21 deletions src/main/java/cyder/audio/CPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import java.util.Objects;

/**
* An encapsulated JLayer {@link Player} for playing singular audio files and stopping.
* An encapsulated JLayer{@link Player} for playing singular audio files and stopping.
*/
public final class CPlayer {
/**
* The audio file.
* The audio file this player will stream/play.
*/
private final File audioFile;

Expand All @@ -26,7 +26,7 @@ public final class CPlayer {
private FileInputStream fis;

/**
* The buffered input stream for the file input stream.
* The buffered input stream for the FileInputStream.
*/
private BufferedInputStream bis;

Expand All @@ -36,7 +36,7 @@ public final class CPlayer {
private Player player;

/**
* The runnable to invoke upon a completion event.
* The runnables to invoke upon a completion event.
*/
private final ArrayList<Runnable> onCompletionCallback;

Expand All @@ -51,9 +51,12 @@ public final class CPlayer {
private boolean playing;

/**
* Constructs a new inner player object.
* Constructs a new CPlayer object.
*
* @param audioFile the audio file this player will play
* @throws NullPointerException if the provided audio file is null
* @throws IllegalArgumentException if the provided audio file does not exist
* or is not a file or is not a supported audio file
*/
public CPlayer(File audioFile) {
Preconditions.checkNotNull(audioFile);
Expand All @@ -69,17 +72,20 @@ public CPlayer(File audioFile) {
* Plays the encapsulated audio file.
*/
public void play() {
Preconditions.checkArgument(!playing);

playing = true;
canceled = false;

CyderThreadRunner.submit(() -> {
try {
canceled = false;
playing = true;
fis = new FileInputStream(audioFile);
bis = new BufferedInputStream(fis);
player = new Player(bis);
player.play();
if (!canceled) onCompletionCallback.forEach(Runnable::run);
} catch (FileNotFoundException | JavaLayerException e) {
e.printStackTrace();
throw new CPlayerException("Failed to play audio file, exception: " + e.getMessage());
} finally {
closeResources();
playing = false;
Expand All @@ -88,17 +94,17 @@ public void play() {
}

/**
* Cancels this player, the on completion callback will not be invoked if set.
* Cancels this player, the on completion callbacks will not be invoked if present.
*/
public void cancel() {
public void cancelPlaying() {
canceled = true;
closeResources();
}

/**
* Stops the player, the completion callback will be invoked if set.
* Stops the player, the completion callbacks will be invoked if present.
*/
public void stop() {
public void stopPlaying() {
closeResources();
}

Expand Down Expand Up @@ -159,15 +165,6 @@ public boolean isUsing(File audioFile) {
return this.audioFile.equals(audioFile);
}

/**
* Returns whether the audio file encapsulated by this player is a system audio file.
*
* @return whether the audio file encapsulated by this player is a system audio file
*/
public boolean isSystemAudio() {
return GeneralAudioPlayer.isSystemAudio(audioFile);
}

/**
* {@inheritDoc}
*/
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/cyder/audio/CPlayerException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cyder.audio;

/**
* An exception for {@link CPlayer}s.
*/
public class CPlayerException extends RuntimeException {
/**
* Constructs a new CPlayerException using the provided error message.
*/
public CPlayerException(String errorMessage) {
super(errorMessage);
}

/**
* Constructs a new CPlayerException from the provided exception.
*/
public CPlayerException(Exception e) {
super(e);
}
}
2 changes: 1 addition & 1 deletion src/main/java/cyder/audio/GeneralAudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static boolean stopAudio(File audioFile) {
* any system audio or AudioPlayer widget audio.
*/
public static void stopGeneralAudio() {
if (generalPlayer != null) generalPlayer.stop();
if (generalPlayer != null) generalPlayer.stopPlaying();
Console.INSTANCE.revalidateAudioMenuVisibility();
}

Expand Down

0 comments on commit f210ea4

Please sign in to comment.