mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Toegevoegd:
-Play Button + CSS + Events -Previous Button + CSS + Events -Changed interface for AudioResource to include a pause button which works really well with mediaplayer, however now SoundEffectAsset has an unnessescary pause
This commit is contained in:
@@ -28,6 +28,8 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
|
||||
.listen(this::handleStopMusicManager)
|
||||
.listen(this::handlePlaySound)
|
||||
.listen(this::handleSkipSong)
|
||||
.listen(this::handlePauseSong)
|
||||
.listen(this::handlePreviousSong)
|
||||
.listen(this::handleStopSound)
|
||||
.listen(this::handleMusicStart)
|
||||
.listen(this::handleVolumeChange)
|
||||
@@ -50,6 +52,15 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
|
||||
this.musicManager.skip();
|
||||
}
|
||||
|
||||
private void handlePauseSong(AudioEvents.PauseMusic event) {
|
||||
this.musicManager.pause();
|
||||
|
||||
}
|
||||
|
||||
private void handlePreviousSong(AudioEvents.PreviousMusic event) {
|
||||
this.musicManager.previous();
|
||||
}
|
||||
|
||||
private void handleStopSound(AudioEvents.StopEffect event) {
|
||||
this.soundEffectManager.stop(event.fileName());
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
|
||||
private final List<T> resources;
|
||||
private int playingIndex = 0;
|
||||
private boolean playing = false;
|
||||
private long pausedPosition = 0;
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
|
||||
@@ -156,4 +157,29 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
|
||||
playing = false;
|
||||
dispatcher.run(() -> backgroundMusic.forEach(T::stop));
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
T current = backgroundMusic.get(playingIndex);
|
||||
if (this.playing) {
|
||||
current.pause();
|
||||
playing = false;
|
||||
}
|
||||
else {
|
||||
this.playing = true;
|
||||
dispatcher.run(() -> {
|
||||
current.play();
|
||||
setTrackRunnable(current);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void previous() {
|
||||
if (backgroundMusic.isEmpty()) return;
|
||||
if (playingIndex == 0) return;
|
||||
stop();
|
||||
scheduler.shutdownNow();
|
||||
playingIndex = playingIndex - 1;
|
||||
playing = true;
|
||||
playCurrentTrack();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ public class AudioEvents extends EventsBase {
|
||||
/** Skips the song to the last second of the song resulting in a skip effect */
|
||||
public record SkipMusic() implements GenericEvent {}
|
||||
|
||||
public record PreviousMusic() implements GenericEvent {}
|
||||
|
||||
public record PauseMusic() implements GenericEvent {}
|
||||
|
||||
/** Change volume, choose type with {@link VolumeControl}. */
|
||||
public record ChangeVolume(double newVolume, VolumeControl controlType) implements GenericEvent {}
|
||||
|
||||
|
||||
@@ -6,4 +6,6 @@ public interface MusicManager<T extends AudioResource> extends AudioManager<T> {
|
||||
void play();
|
||||
void stop();
|
||||
void skip();
|
||||
void previous();
|
||||
void pause();
|
||||
}
|
||||
|
||||
@@ -78,6 +78,10 @@ public class MusicAsset extends BaseResource implements LoadableResource, AudioR
|
||||
getMediaPlayer().play();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
getMediaPlayer().pause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
getMediaPlayer().stop();
|
||||
|
||||
@@ -142,6 +142,12 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource,
|
||||
if (this.clip.isRunning()) this.clip.stop();
|
||||
}
|
||||
|
||||
// had to be implemented for mediaplayer.pause() to work so:
|
||||
// TODO: get this removed, somehow OR get a clip.pause which I have no idea why you would ever use
|
||||
public void pause() {
|
||||
this.clip.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long duration() {
|
||||
if (clip != null) {
|
||||
|
||||
@@ -9,4 +9,5 @@ public interface AudioResource {
|
||||
long currentPosition();
|
||||
void setOnEnd(Runnable run);
|
||||
void setOnError(Runnable run);
|
||||
void pause();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user