Skip Button

This commit is contained in:
michiel
2025-10-16 13:35:41 +02:00
parent abc173a1ee
commit 30f797022c
12 changed files with 97 additions and 30 deletions

View File

@@ -26,6 +26,7 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
new EventFlow()
.listen(this::handleStopMusicManager)
.listen(this::handlePlaySound)
.listen(this::handleSkipSong)
.listen(this::handleStopSound)
.listen(this::handleMusicStart)
.listen(this::handleVolumeChange)
@@ -44,6 +45,10 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
this.soundEffectManager.play(event.fileName(), event.loop());
}
private void handleSkipSong(AudioEvents.SkipMusic event) {
this.musicManager.skip();
}
private void handleStopSound(AudioEvents.StopEffect event) {
this.soundEffectManager.stop(event.fileName());
}

View File

@@ -23,6 +23,8 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
private final List<T> resources;
private int playingIndex = 0;
private boolean playing = false;
private ScheduledExecutorService scheduler;
public MusicManager(List<T> resources) {
this.dispatcher = new JavaFXDispatcher();
@@ -69,6 +71,15 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
playCurrentTrack();
}
public void skip() {
if (backgroundMusic.isEmpty()) return;
stop();
scheduler.shutdownNow();
playingIndex = playingIndex + 1;
playing = true;
playCurrentTrack();
}
// Used in testing
void play(int index) {
if (playing) {
@@ -104,8 +115,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
private void setTrackRunnable(T track) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler = Executors.newSingleThreadScheduledExecutor();
Runnable currentMusicTask = new Runnable() {
@Override

View File

@@ -21,6 +21,9 @@ public class AudioEvents extends EventsBase {
/** Gives back the name of the song, the position its currently at (in seconds) and how long it takes (in seconds) */
public record PlayingMusic(String name, long currentPosition, long duration) implements GenericEvent {}
/** Skips the song to the last second of the song resulting in a skip effect */
public record SkipMusic() implements GenericEvent {}
/** Change volume, choose type with {@link VolumeControl}. */
public record ChangeVolume(double newVolume, VolumeControl controlType) implements GenericEvent {}

View File

@@ -5,4 +5,5 @@ import org.toop.framework.resource.types.AudioResource;
public interface MusicManager<T extends AudioResource> extends AudioManager<T> {
void play();
void stop();
void skip();
}