mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
83 spelen van geluid geeft id terug (#86)
* Between commit
* Revert "Between commit"
This reverts commit 6c6e55492e.
* Removed debug print from main
* Removed class variables that are no longer used. Sound resources now generate a new Clip with the getNewClip() method.
* Successfully plays multiple instances of the same sound. Returns the clipID so the sound can be stopped.
* Added some comments
This commit is contained in:
@@ -14,14 +14,17 @@ import java.io.IOException;
|
|||||||
import java.nio.file.NotDirectoryException;
|
import java.nio.file.NotDirectoryException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
|
||||||
var a = new AssetManager(new File("app/src/main/resources/assets"));
|
var a = new AssetManager(new File("app/src/main/resources/assets"));
|
||||||
var b = new NetworkingClientManager();
|
var b = new NetworkingClientManager();
|
||||||
var c = new SoundManager(a);
|
var c = new SoundManager(a);
|
||||||
|
|
||||||
// IO.println(a.getAssets());
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||||
IO.println(c.getClips());
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
||||||
|
Thread.sleep(200);
|
||||||
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||||
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
||||||
|
Thread.sleep(200);
|
||||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
||||||
|
|
||||||
|
|||||||
@@ -4,32 +4,24 @@ import javax.sound.sampled.*;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class AudioResource extends Resource {
|
public class AudioResource extends Resource {
|
||||||
private AudioInputStream audioInputStream = null;
|
|
||||||
private Clip clip = null;
|
// Constructor
|
||||||
public AudioResource(File audioFile) {
|
public AudioResource(File audioFile) {
|
||||||
super(audioFile);
|
super(audioFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AudioInputStream getAudioStream(){
|
// Gets a new clip to play
|
||||||
return this.audioInputStream;
|
public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
|
||||||
}
|
// Get a new clip from audio system
|
||||||
|
|
||||||
public Clip getClip() {
|
|
||||||
return this.clip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Resource load() {
|
|
||||||
try {
|
|
||||||
this.audioInputStream = AudioSystem.getAudioInputStream(this.getStream());
|
|
||||||
Clip clip = AudioSystem.getClip();
|
Clip clip = AudioSystem.getClip();
|
||||||
clip.open(this.audioInputStream);
|
|
||||||
this.clip = clip;
|
// Insert a new audio stream into the clip
|
||||||
} catch (UnsupportedAudioFileException | LineUnavailableException e) {
|
clip.open(this.getAudioStream());
|
||||||
throw new RuntimeException(e);
|
return clip;
|
||||||
} catch (IOException e) { // TODO: Error handling
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
return this;
|
|
||||||
|
// Generates a new audio stream from byte array
|
||||||
|
private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
|
||||||
|
return AudioSystem.getAudioInputStream(this.getStream());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package org.toop.framework.audio;
|
package org.toop.framework.audio;
|
||||||
|
|
||||||
|
import org.toop.framework.SnowflakeGenerator;
|
||||||
import org.toop.framework.assets.Asset;
|
import org.toop.framework.assets.Asset;
|
||||||
import org.toop.framework.assets.AssetManager;
|
import org.toop.framework.assets.AssetManager;
|
||||||
import org.toop.framework.assets.resources.AudioResource;
|
import org.toop.framework.assets.resources.AudioResource;
|
||||||
import org.toop.framework.assets.resources.Resource;
|
|
||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
|
||||||
@@ -12,16 +12,15 @@ import java.util.*;
|
|||||||
import javax.sound.sampled.*;
|
import javax.sound.sampled.*;
|
||||||
|
|
||||||
public class SoundManager {
|
public class SoundManager {
|
||||||
private final Map<String, Clip> activeClips = new HashMap<>();
|
private final Map<Long, Clip> activeClips = new HashMap<>();
|
||||||
private final HashMap<String, Clip> clips = new HashMap<>();
|
private final HashMap<String, AudioResource> audioResources = new HashMap<>();
|
||||||
private final AssetManager assetManager;
|
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
|
||||||
|
|
||||||
public SoundManager(AssetManager asm) {
|
public SoundManager(AssetManager asm) {
|
||||||
this.assetManager = asm;
|
|
||||||
// Get all Audio Resources and add them to a list.
|
// Get all Audio Resources and add them to a list.
|
||||||
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
|
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
|
||||||
try {
|
try {
|
||||||
addClip(resource);
|
this.addAudioResource(resource);
|
||||||
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -36,28 +35,29 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleStopSound(AudioEvents.StopAudio event) {
|
private void handleStopSound(AudioEvents.StopAudio event) {
|
||||||
this.stopSound(event.fileNameNoExtensionAndNoDirectory());
|
this.stopSound(event.clipId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addClip(Asset<AudioResource> audioAsset)
|
private void addAudioResource(Asset<AudioResource> audioAsset)
|
||||||
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
||||||
AudioResource audioResource = audioAsset.getResource();
|
AudioResource audioResource = audioAsset.getResource();
|
||||||
|
|
||||||
this.clips.put(audioAsset.getName(), audioResource.getClip());
|
this.audioResources.put(audioAsset.getName(), audioResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playSound(String audioFileName, boolean loop) {
|
private long playSound(String audioFileName, boolean loop) {
|
||||||
// Get clip
|
try {
|
||||||
Clip clip = clips.get(audioFileName);
|
AudioResource resource = audioResources.get(audioFileName);
|
||||||
|
|
||||||
if (clip == null) {
|
// Return -1 which indicates resource wasn't available
|
||||||
return;
|
if (resource == null){
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset clip
|
// Get a new clip from resource
|
||||||
clip.setFramePosition(0);
|
Clip clip = resource.getNewClip();
|
||||||
|
|
||||||
// If loop make it loop, else just start it once
|
// If supposed to loop make it loop, else just start it once
|
||||||
if (loop){
|
if (loop){
|
||||||
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
||||||
}
|
}
|
||||||
@@ -65,24 +65,29 @@ public class SoundManager {
|
|||||||
clip.start();
|
clip.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate id for clip
|
||||||
|
long clipId = idGenerator.nextId();
|
||||||
|
|
||||||
// store it so we can stop it later
|
// 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)
|
// remove when finished (only for non-looping sounds)
|
||||||
clip.addLineListener(event -> {
|
clip.addLineListener(event -> {
|
||||||
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
|
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
|
||||||
activeClips.remove(audioFileName);
|
activeClips.remove(clipId);
|
||||||
clip.close();
|
clip.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Return id so it can be stopped
|
||||||
|
return clipId;
|
||||||
|
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, Clip> getClips() {
|
public void stopSound(long clipId) {
|
||||||
return this.clips;
|
Clip clip = activeClips.get(clipId);
|
||||||
}
|
|
||||||
|
|
||||||
public void stopSound(String audioFileName) {
|
|
||||||
Clip clip = activeClips.get(audioFileName);
|
|
||||||
|
|
||||||
if (clip == null) {
|
if (clip == null) {
|
||||||
return;
|
return;
|
||||||
@@ -90,7 +95,7 @@ public class SoundManager {
|
|||||||
|
|
||||||
clip.stop();
|
clip.stop();
|
||||||
clip.close();
|
clip.close();
|
||||||
activeClips.remove(audioFileName);
|
activeClips.remove(clipId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopAllSounds() {
|
public void stopAllSounds() {
|
||||||
|
|||||||
@@ -8,5 +8,5 @@ public class AudioEvents extends EventsBase {
|
|||||||
public record PlayAudio(String fileNameNoExtensionAndNoDirectory, boolean loop)
|
public record PlayAudio(String fileNameNoExtensionAndNoDirectory, boolean loop)
|
||||||
implements EventWithoutSnowflake {}
|
implements EventWithoutSnowflake {}
|
||||||
|
|
||||||
public record StopAudio(String fileNameNoExtensionAndNoDirectory) implements EventWithoutSnowflake {}
|
public record StopAudio(long clipId) implements EventWithoutSnowflake {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user