Between commit

This commit is contained in:
2025-09-30 18:40:32 +02:00
parent 44b4613c27
commit 6c6e55492e
2 changed files with 30 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
package org.toop.framework.audio;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.assets.Asset;
import org.toop.framework.assets.AssetManager;
import org.toop.framework.assets.resources.AudioResource;
@@ -12,12 +13,12 @@ import java.util.*;
import javax.sound.sampled.*;
public class SoundManager {
private final Map<String, Clip> activeClips = new HashMap<>();
private final HashMap<String, Clip> clips = new HashMap<>();
private final AssetManager assetManager;
private final Map<Long, Clip> activeClips = new HashMap<>();
private final HashMap<String, AudioInputStream> audioStreams = new HashMap<>();
private final SnowflakeGenerator idGenerator;
public SoundManager(AssetManager asm) {
this.assetManager = asm;
this.idGenerator = new SnowflakeGenerator(); // TODO: don't create a new snowflake generator.
// Get all Audio Resources and add them to a list.
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
try {
@@ -32,7 +33,11 @@ public class SoundManager {
}
private void handlePlaySound(AudioEvents.PlayAudio event) {
this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop());
try {
this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop());
} catch (LineUnavailableException | IOException e) {
throw new RuntimeException(e);
}
}
private void handleStopSound(AudioEvents.StopAudio event) {
@@ -42,20 +47,22 @@ public class SoundManager {
private void addClip(Asset<AudioResource> audioAsset)
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioResource audioResource = audioAsset.getResource();
this.clips.put(audioAsset.getName(), audioResource.getClip());
this.audioStreams.put(audioAsset.getName(), audioResource.getAudioStream());
}
private void playSound(String audioFileName, boolean loop) {
// Get clip
Clip clip = clips.get(audioFileName);
private long playSound(String audioFileName, boolean loop) throws LineUnavailableException, IOException {
// Get audioStream
AudioInputStream audioStream = audioStreams.get(audioFileName);
if (clip == null) {
return;
// Return -1 if audiStream doesn't exist
if (audioStream == null) {
return -1;
}
// Reset clip
clip.setFramePosition(0);
// Get a clip and open the audioStream
final Clip clip = AudioSystem.getClip();
clip.open(audioStream);
// If loop make it loop, else just start it once
if (loop){
@@ -65,20 +72,25 @@ public class SoundManager {
clip.start();
}
// Generate ID
long clipId = idGenerator.nextId();
// store it so we can stop it later
activeClips.put(audioFileName, clip); // TODO: Do on snowflake for specific sound to stop
activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop
// remove when finished (only for non-looping sounds)
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
activeClips.remove(audioFileName);
activeClips.remove(clipId);
clip.close();
}
});
return clipId;
}
public HashMap<String, Clip> getClips() {
return this.clips;
public HashMap<String, AudioInputStream> getAudioStreams() {
return this.audioStreams;
}
public void stopSound(String audioFileName) {