diff --git a/src/main/java/cyder/audio/CPlayer.java b/src/main/java/cyder/audio/CPlayer.java index 968f2a995..060b39691 100644 --- a/src/main/java/cyder/audio/CPlayer.java +++ b/src/main/java/cyder/audio/CPlayer.java @@ -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; @@ -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; @@ -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 onCompletionCallback; @@ -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); @@ -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; @@ -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(); } @@ -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} */ diff --git a/src/main/java/cyder/audio/CPlayerException.java b/src/main/java/cyder/audio/CPlayerException.java new file mode 100644 index 000000000..0ef54d17d --- /dev/null +++ b/src/main/java/cyder/audio/CPlayerException.java @@ -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); + } +} diff --git a/src/main/java/cyder/audio/GeneralAudioPlayer.java b/src/main/java/cyder/audio/GeneralAudioPlayer.java index 47caec867..cba0ce6cf 100644 --- a/src/main/java/cyder/audio/GeneralAudioPlayer.java +++ b/src/main/java/cyder/audio/GeneralAudioPlayer.java @@ -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(); }