Updated SoundManager.java to simplify how audios are played. Uses new AssetManager and AudioInputStream instead of whatever was going on at first.

This commit is contained in:
2025-09-30 16:58:26 +02:00
parent 423fd9d68a
commit 44b4613c27

View File

@@ -12,31 +12,17 @@ import java.util.*;
import javax.sound.sampled.*; import javax.sound.sampled.*;
public class SoundManager { public class SoundManager {
private javax.sound.sampled.Line.Info lineInfo;
private final Map<String, Clip> activeClips = new HashMap<>(); private final Map<String, Clip> activeClips = new HashMap<>();
private HashMap<String, Integer> clips = new HashMap<>(); private final HashMap<String, Clip> clips = new HashMap<>();
private AssetManager assetManager; private final AssetManager assetManager;
private Vector afs;
private Vector sizes;
private Vector infos;
private Vector audios;
private int num=0;
public SoundManager(AssetManager ass) { public SoundManager(AssetManager asm) {
afs=new Vector(); this.assetManager = asm;
sizes=new Vector(); // Get all Audio Resources and add them to a list.
infos=new Vector(); for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
audios=new Vector();
this.assetManager = ass;
for (Asset<AudioResource> resource : ass.getAllResourceOfType(AudioResource.class).values()) {
try { try {
addClip(resource); addClip(resource);
} catch (IOException e) { } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@@ -53,81 +39,59 @@ public class SoundManager {
this.stopSound(event.fileNameNoExtensionAndNoDirectory()); this.stopSound(event.fileNameNoExtensionAndNoDirectory());
} }
private void addClip(Asset<AudioResource> audiol) private void addClip(Asset<AudioResource> audioAsset)
throws IOException, UnsupportedAudioFileException, LineUnavailableException { throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioResource ad = audiol.getResource(); AudioResource audioResource = audioAsset.getResource();
AudioFormat af = ad.getAudioStream().getFormat();
int size = (int) (af.getFrameSize() * ad.getAudioStream().getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
ad.getInputStream().read(audio, 0, size);
afs.add(af); this.clips.put(audioAsset.getName(), audioResource.getClip());
sizes.add(new Integer(size));
infos.add(info);
audios.add(audio);
this.clips.put(audiol.getName(), this.audios.size()-1);
num++;
}
private ByteArrayInputStream loadStream(InputStream inputstream)
throws IOException
{
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte data[] = new byte[1024];
for(int i = inputstream.read(data); i != -1; i = inputstream.read(data))
bytearrayoutputstream.write(data, 0, i);
inputstream.close();
bytearrayoutputstream.close();
data = bytearrayoutputstream.toByteArray();
return new ByteArrayInputStream(data);
} }
private void playSound(String audioFileName, boolean loop) { private void playSound(String audioFileName, boolean loop) {
var b = this.assetManager.getAllResourceOfType(AudioResource.class); // Get clip
b.get(audioFileName).getResource().getClip().start(); Clip clip = clips.get(audioFileName);
if (clip == null) {
return;
} }
// private void playSound(String audioFileName, boolean loop) // Reset clip
// throws UnsupportedAudioFileException, LineUnavailableException { clip.setFramePosition(0);
// int x = clips.get(audioFileName);
// if (x > num) {
// System.out.println("playSound: sample nr[" + x + "] is not available");
// } else {
// Clip clip = (Clip) AudioSystem.getLine((DataLine.Info) infos.elementAt(x));
//// clip.open((AudioFormat) afs.elementAt(x), (byte[]) audios.elementAt(x),
//// 0, ((Integer) sizes.elementAt(x)).intValue());
//
// clip.start();
// if (loop) clip.loop(Clip.LOOP_CONTINUOUSLY);
//
// // store it so we can stop it later
// activeClips.put(audioFileName, 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);
// clip.close();
// }
// });
// }
// }
public HashMap<String, Integer> getClips() { // If loop make it loop, else just start it once
if (loop){
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
clip.start();
}
// store it so we can stop it later
activeClips.put(audioFileName, 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);
clip.close();
}
});
}
public HashMap<String, Clip> getClips() {
return this.clips; return this.clips;
} }
public void stopSound(String audioFileName) { public void stopSound(String audioFileName) {
Clip clip = activeClips.get(audioFileName); Clip clip = activeClips.get(audioFileName);
if (clip != null) {
if (clip == null) {
return;
}
clip.stop(); clip.stop();
clip.close(); clip.close();
activeClips.remove(audioFileName); activeClips.remove(audioFileName);
} }
}
public void stopAllSounds() { public void stopAllSounds() {
for (Clip clip : activeClips.values()) { for (Clip clip : activeClips.values()) {