Skip to content

Commit

Permalink
(#23) Added pause, resume, and stop audio actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed May 31, 2021
1 parent 8df8d33 commit 53f81b9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
34 changes: 24 additions & 10 deletions src/main/java/tech/fastj/systems/audio/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -29,13 +30,8 @@ public class Audio {
() -> {
},
() -> {
clip.stop();
clip.flush();
clip.drain();
clip.close();
}
);

clip.addLineListener(simpleLineListener);
}

Expand All @@ -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);
}
Expand All @@ -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 +
'}';
}
}
70 changes: 61 additions & 9 deletions src/main/java/tech/fastj/systems/audio/AudioManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand All @@ -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();
Expand All @@ -68,25 +82,63 @@ 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) {
FastJEngine.error(CrashMessages.theGameCrashed("an error while trying to play sound."), exception);
}
});
}

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();
}
});
}
}

0 comments on commit 53f81b9

Please sign in to comment.