diff --git a/src/main/java/tech/fastj/systems/audio/AudioEventListener.java b/src/main/java/tech/fastj/systems/audio/AudioEventListener.java index cea3662e..c64f05fc 100644 --- a/src/main/java/tech/fastj/systems/audio/AudioEventListener.java +++ b/src/main/java/tech/fastj/systems/audio/AudioEventListener.java @@ -3,45 +3,46 @@ import javax.sound.sampled.LineEvent; import java.util.Map; import java.util.Objects; +import java.util.function.BiConsumer; import java.util.function.Consumer; /** An event listener for the {@link MemoryAudio} class. */ public class AudioEventListener { - private Runnable audioOpenAction; - private Runnable audioCloseAction; - private Runnable audioStartAction; - private Runnable audioStopAction; - private Runnable audioPauseAction; - private Runnable audioResumeAction; + private Consumer audioOpenAction; + private Consumer audioCloseAction; + private Consumer audioStartAction; + private Consumer audioStopAction; + private Consumer audioPauseAction; + private Consumer audioResumeAction; private final Audio audio; - private static final Map> AudioEventProcessor = Map.of( - LineEvent.Type.OPEN, audioEventListener -> audioEventListener.audioOpenAction.run(), - LineEvent.Type.START, audioEventListener -> { + private static final Map> AudioEventProcessor = Map.of( + LineEvent.Type.OPEN, (audioEvent, audioEventListener) -> audioEventListener.audioOpenAction.accept(audioEvent), + LineEvent.Type.START, (audioEvent, audioEventListener) -> { switch (audioEventListener.audio.getPreviousPlaybackState()) { case Paused: { - audioEventListener.audioResumeAction.run(); + audioEventListener.audioResumeAction.accept(audioEvent); } case Stopped: { - audioEventListener.audioStartAction.run(); + audioEventListener.audioStartAction.accept(audioEvent); break; } } }, - LineEvent.Type.STOP, audioEventListener -> { + LineEvent.Type.STOP, (audioEvent, audioEventListener) -> { switch (audioEventListener.audio.getCurrentPlaybackState()) { case Paused: { - audioEventListener.audioPauseAction.run(); + audioEventListener.audioPauseAction.accept(audioEvent); } case Stopped: { - audioEventListener.audioStopAction.run(); + audioEventListener.audioStopAction.accept(audioEvent); break; } } }, - LineEvent.Type.CLOSE, audioEventListener -> audioEventListener.audioCloseAction.run() + LineEvent.Type.CLOSE, (audioEvent, audioEventListener) -> audioEventListener.audioCloseAction.accept(audioEvent) ); /** @@ -59,7 +60,7 @@ public class AudioEventListener { * * @return The "audio open" event action. */ - public Runnable getAudioOpenAction() { + public Consumer getAudioOpenAction() { return audioOpenAction; } @@ -68,7 +69,7 @@ public Runnable getAudioOpenAction() { * * @return The "audio close" event action. */ - public Runnable getAudioCloseAction() { + public Consumer getAudioCloseAction() { return audioCloseAction; } @@ -77,7 +78,7 @@ public Runnable getAudioCloseAction() { * * @return The "audio start" event action. */ - public Runnable getAudioStartAction() { + public Consumer getAudioStartAction() { return audioStartAction; } @@ -86,7 +87,7 @@ public Runnable getAudioStartAction() { * * @return The "audio stop" event action. */ - public Runnable getAudioStopAction() { + public Consumer getAudioStopAction() { return audioStopAction; } @@ -95,7 +96,7 @@ public Runnable getAudioStopAction() { * * @return The "audio pause" event action. */ - public Runnable getAudioPauseAction() { + public Consumer getAudioPauseAction() { return audioPauseAction; } @@ -104,7 +105,7 @@ public Runnable getAudioPauseAction() { * * @return The "audio resume" event action. */ - public Runnable getAudioResumeAction() { + public Consumer getAudioResumeAction() { return audioResumeAction; } @@ -113,7 +114,7 @@ public Runnable getAudioResumeAction() { * * @param audioOpenAction The action to set. */ - public void setAudioOpenAction(Runnable audioOpenAction) { + public void setAudioOpenAction(Consumer audioOpenAction) { this.audioOpenAction = audioOpenAction; } @@ -122,7 +123,7 @@ public void setAudioOpenAction(Runnable audioOpenAction) { * * @param audioCloseAction The action to set. */ - public void setAudioCloseAction(Runnable audioCloseAction) { + public void setAudioCloseAction(Consumer audioCloseAction) { this.audioCloseAction = audioCloseAction; } @@ -131,7 +132,7 @@ public void setAudioCloseAction(Runnable audioCloseAction) { * * @param audioStartAction The action to set. */ - public void setAudioStartAction(Runnable audioStartAction) { + public void setAudioStartAction(Consumer audioStartAction) { this.audioStartAction = audioStartAction; } @@ -140,7 +141,7 @@ public void setAudioStartAction(Runnable audioStartAction) { * * @param audioStopAction The action to set. */ - public void setAudioStopAction(Runnable audioStopAction) { + public void setAudioStopAction(Consumer audioStopAction) { this.audioStopAction = audioStopAction; } @@ -149,7 +150,7 @@ public void setAudioStopAction(Runnable audioStopAction) { * * @param audioPauseAction The action to set. */ - public void setAudioPauseAction(Runnable audioPauseAction) { + public void setAudioPauseAction(Consumer audioPauseAction) { this.audioPauseAction = audioPauseAction; } @@ -158,7 +159,7 @@ public void setAudioPauseAction(Runnable audioPauseAction) { * * @param audioResumeAction The action to set. */ - public void setAudioResumeAction(Runnable audioResumeAction) { + public void setAudioResumeAction(Consumer audioResumeAction) { this.audioResumeAction = audioResumeAction; } @@ -168,6 +169,6 @@ public void setAudioResumeAction(Runnable audioResumeAction) { * @param audioEvent The event fired. */ public void fireEvent(LineEvent audioEvent) { - AudioEventProcessor.get(audioEvent.getType()).accept(this); + AudioEventProcessor.get(audioEvent.getType()).accept(audioEvent, this); } } diff --git a/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java b/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java index b5844875..04271de7 100644 --- a/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java +++ b/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java @@ -122,8 +122,8 @@ void checkPlayMemoryAudio_shouldTriggerOpenAndStartEvents() throws InterruptedEx MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath); AtomicBoolean audioOpenEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStartEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioOpenAction(() -> audioOpenEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(true)); + audio.getAudioEventListener().setAudioOpenAction(audioEvent -> audioOpenEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -137,8 +137,8 @@ void checkPauseMemoryAudio_shouldTriggerPauseAndStopEvents() throws InterruptedE MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath); AtomicBoolean audioPauseEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioPauseAction(() -> audioPauseEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true)); + audio.getAudioEventListener().setAudioPauseAction(audioEvent -> audioPauseEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -154,8 +154,8 @@ void checkResumeMemoryAudio_shouldTriggerStartAndResumeEvents() throws Interrupt MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath); AtomicBoolean audioStartEventBoolean = new AtomicBoolean(true); AtomicBoolean audioResumeEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(!audioStartEventBoolean.get())); - audio.getAudioEventListener().setAudioResumeAction(() -> audioResumeEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(!audioStartEventBoolean.get())); + audio.getAudioEventListener().setAudioResumeAction(audioEvent -> audioResumeEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -173,8 +173,8 @@ void checkStopMemoryAudio_shouldTriggerStopAndCloseEvents() throws InterruptedEx MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath); AtomicBoolean audioCloseEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioCloseAction(() -> audioCloseEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true)); + audio.getAudioEventListener().setAudioCloseAction(audioEvent -> audioCloseEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); diff --git a/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java b/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java index 5b3a1925..9fce0c04 100644 --- a/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java +++ b/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java @@ -42,8 +42,8 @@ void checkPlayStreamedAudio_shouldTriggerOpenAndStartEvents() throws Interrupted StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath); AtomicBoolean audioOpenEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStartEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioOpenAction(() -> audioOpenEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(true)); + audio.getAudioEventListener().setAudioOpenAction(audioEvent -> audioOpenEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -57,8 +57,8 @@ void checkPauseStreamedAudio_shouldTriggerPauseAndStopEvents() throws Interrupte StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath); AtomicBoolean audioPauseEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioPauseAction(() -> audioPauseEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true)); + audio.getAudioEventListener().setAudioPauseAction(audioEvent -> audioPauseEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -74,8 +74,8 @@ void checkResumeStreamedAudio_shouldTriggerStartAndResumeEvents() throws Interru StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath); AtomicBoolean audioStartEventBoolean = new AtomicBoolean(true); AtomicBoolean audioResumeEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(!audioStartEventBoolean.get())); - audio.getAudioEventListener().setAudioResumeAction(() -> audioResumeEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(!audioStartEventBoolean.get())); + audio.getAudioEventListener().setAudioResumeAction(audioEvent -> audioResumeEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3); @@ -93,8 +93,8 @@ void checkStopStreamedAudio_shouldTriggerStopAndCloseEvents() throws Interrupted StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath); AtomicBoolean audioCloseEventBoolean = new AtomicBoolean(false); AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false); - audio.getAudioEventListener().setAudioCloseAction(() -> audioCloseEventBoolean.set(true)); - audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true)); + audio.getAudioEventListener().setAudioCloseAction(audioEvent -> audioCloseEventBoolean.set(true)); + audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true)); audio.play(); TimeUnit.MILLISECONDS.sleep(3);