From 53f81b92d74a606fc2bc7ecb89f2f4a51c8842cc Mon Sep 17 00:00:00 2001 From: lucasstarsz Date: Mon, 31 May 2021 17:03:09 -0400 Subject: [PATCH] (#23) Added pause, resume, and stop audio actions --- .../java/tech/fastj/systems/audio/Audio.java | 34 ++++++--- .../fastj/systems/audio/AudioManager.java | 70 ++++++++++++++++--- 2 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/main/java/tech/fastj/systems/audio/Audio.java b/src/main/java/tech/fastj/systems/audio/Audio.java index 3fb20ae6..ca715b16 100644 --- a/src/main/java/tech/fastj/systems/audio/Audio.java +++ b/src/main/java/tech/fastj/systems/audio/Audio.java @@ -2,22 +2,23 @@ import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.Clip; -import javax.sound.sampled.FloatControl; -import java.net.URL; import java.nio.file.Path; -import java.util.Arrays; import java.util.Objects; +import java.util.concurrent.TimeUnit; public class Audio { /** Signifies that the audio should loop when it finishes playing. */ - public static final int LoopWhenFinishedPlaying = -1; + public static final int ContinuousLoop = Clip.LOOP_CONTINUOUSLY; private final Clip clip; private final AudioInputStream audioInputStream; private final SimpleLineListener simpleLineListener; + private final Path audioPath; Audio(Path audioPath) { + this.audioPath = audioPath; + clip = Objects.requireNonNull(AudioManager.newClip()); audioInputStream = AudioManager.newAudioStream(audioPath); @@ -29,13 +30,8 @@ public class Audio { () -> { }, () -> { - clip.stop(); - clip.flush(); - clip.drain(); - clip.close(); } ); - clip.addLineListener(simpleLineListener); } @@ -51,6 +47,10 @@ public SimpleLineListener getSimpleLineListener() { return simpleLineListener; } + public Path getAudioPath() { + return audioPath; + } + public void setLoopPoints(int loopStart, int loopEnd) { clip.setLoopPoints(loopStart, loopEnd); } @@ -63,11 +63,25 @@ public void play() { AudioManager.playAudio(this); } + public void pause() { + AudioManager.pauseAudio(this); + } + + public void resume() { + AudioManager.resumeAudio(this); + } + + public void stop() { + AudioManager.stopAudio(this); + } + @Override public String toString() { return "Audio{" + "clip=" + clip + - ", audioInputStream=" + audioInputStream + + ", audioPath=\"" + audioPath.toString() + + "\", audioInputStream=" + audioInputStream + + ", simpleLineListener=" + simpleLineListener + '}'; } } diff --git a/src/main/java/tech/fastj/systems/audio/AudioManager.java b/src/main/java/tech/fastj/systems/audio/AudioManager.java index 3c3ec029..4d14ad5e 100644 --- a/src/main/java/tech/fastj/systems/audio/AudioManager.java +++ b/src/main/java/tech/fastj/systems/audio/AudioManager.java @@ -10,7 +10,6 @@ import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import java.io.IOException; -import java.net.URL; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; @@ -37,6 +36,12 @@ public static void loadAudio(Path... audioPaths) { } } + public static void unloadAudio(Path... audioPaths) { + for (Path audioPath : audioPaths) { + AudioFiles.remove(audioPath.toString()); + } + } + public static Audio getAudio(Path audioPath) { return getAudio(audioPath.toString()); } @@ -45,6 +50,15 @@ public static Audio getAudio(String audioPath) { return AudioFiles.get(audioPath); } + public static void unloadAllAudio() { + AudioFiles.clear(); + } + + public static void stop() { + AudioPlayer.shutdownNow(); + unloadAllAudio(); + } + static Clip newClip() { try { return AudioSystem.getClip(); @@ -68,20 +82,19 @@ static AudioInputStream newAudioStream(Path audioPath) { return null; } - - - } - - public static void stop() { - AudioPlayer.shutdownNow(); } static void playAudio(Audio audio) { AudioPlayer.execute(() -> { Clip clip = audio.getClip(); - AudioInputStream audioInputStream = audio.getAudioInputStream(); + + if (clip.isOpen()) { + FastJEngine.warning("Tried to play audio file \"" + audio.getAudioPath().toString() + "\", but it was already open (and likely being used elsewhere.)"); + return; + } + try { - System.out.println("?"); + AudioInputStream audioInputStream = audio.getAudioInputStream(); clip.open(audioInputStream); clip.start(); } catch (LineUnavailableException | IOException exception) { @@ -89,4 +102,43 @@ static void playAudio(Audio audio) { } }); } + + static void pauseAudio(Audio audio) { + AudioPlayer.execute(() -> { + Clip clip = audio.getClip(); + + if (!clip.isOpen()) { + FastJEngine.warning("Tried to pause audio file \"" + audio.getAudioPath().toString() + "\", but it wasn't being played."); + } else { + clip.stop(); + } + }); + } + + static void resumeAudio(Audio audio) { + AudioPlayer.execute(() -> { + Clip clip = audio.getClip(); + + if (!clip.isOpen()) { + FastJEngine.warning("Tried to resume audio file \"" + audio.getAudioPath().toString() + "\", but it wasn't being played."); + } else { + clip.start(); + } + }); + } + + static void stopAudio(Audio audio) { + AudioPlayer.execute(() -> { + Clip clip = audio.getClip(); + + if (!clip.isOpen()) { + FastJEngine.warning("Tried to stop audio file \"" + audio.getAudioPath().toString() + "\", but it wasn't being played."); + } else { + clip.stop(); + clip.flush(); + clip.drain(); + clip.close(); + } + }); + } }