mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Merge remote-tracking branch 'refs/remotes/origin/46-audio-epic' into assetManager
# Conflicts: # framework/src/main/java/org/toop/framework/assets/resources/AudioAsset.java # framework/src/main/java/org/toop/framework/assets/resources/BaseResource.java # framework/src/main/java/org/toop/framework/assets/resources/ImageResource.java
This commit is contained in:
@@ -14,14 +14,17 @@ import java.io.IOException;
|
||||
import java.nio.file.NotDirectoryException;
|
||||
|
||||
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 b = new NetworkingClientManager();
|
||||
var c = new SoundManager(a);
|
||||
|
||||
// IO.println(a.getAssets());
|
||||
IO.println(c.getClips());
|
||||
|
||||
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("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();
|
||||
|
||||
|
||||
@@ -1,23 +1,34 @@
|
||||
package org.toop.framework.assets.resources;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public abstract class BaseResource {
|
||||
|
||||
final InputStream stream;
|
||||
final File file;
|
||||
public abstract class Resource {
|
||||
final private byte[] rawData;
|
||||
final private File file;
|
||||
|
||||
BaseResource(final File file) {
|
||||
Resource(final File file) throws RuntimeException {
|
||||
this.file = file;
|
||||
try {
|
||||
this.stream = new BufferedInputStream(new FileInputStream(file));
|
||||
} catch (FileNotFoundException e) {
|
||||
this.rawData = Files.readAllBytes(file.toPath());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return this.stream;
|
||||
public Resource load() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public InputStream getStream() {
|
||||
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
package org.toop.framework.assets.resources;
|
||||
|
||||
import javafx.scene.image.Image;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ImageResource extends Resource {
|
||||
|
||||
private Image image = null;
|
||||
|
||||
public ImageResource(File imageFile) {
|
||||
super(imageFile);
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return this.image;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource load() {
|
||||
this.image = new Image(this.getStream());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
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;
|
||||
import org.toop.framework.assets.resources.Resource;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
|
||||
@@ -12,16 +12,15 @@ 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, AudioResource> audioResources = new HashMap<>();
|
||||
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
|
||||
|
||||
public SoundManager(AssetManager asm) {
|
||||
this.assetManager = asm;
|
||||
// Get all Audio Resources and add them to a list.
|
||||
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
|
||||
try {
|
||||
addClip(resource);
|
||||
this.addAudioResource(resource);
|
||||
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -36,28 +35,29 @@ public class SoundManager {
|
||||
}
|
||||
|
||||
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 {
|
||||
AudioResource audioResource = audioAsset.getResource();
|
||||
|
||||
this.clips.put(audioAsset.getName(), audioResource.getClip());
|
||||
this.audioResources.put(audioAsset.getName(), audioResource);
|
||||
}
|
||||
|
||||
private void playSound(String audioFileName, boolean loop) {
|
||||
// Get clip
|
||||
Clip clip = clips.get(audioFileName);
|
||||
private long playSound(String audioFileName, boolean loop) {
|
||||
try {
|
||||
AudioResource resource = audioResources.get(audioFileName);
|
||||
|
||||
if (clip == null) {
|
||||
return;
|
||||
// Return -1 which indicates resource wasn't available
|
||||
if (resource == null){
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Reset clip
|
||||
clip.setFramePosition(0);
|
||||
// Get a new clip from resource
|
||||
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){
|
||||
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
||||
}
|
||||
@@ -65,24 +65,29 @@ public class SoundManager {
|
||||
clip.start();
|
||||
}
|
||||
|
||||
// Generate id for clip
|
||||
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 id so it can be stopped
|
||||
return clipId;
|
||||
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, Clip> getClips() {
|
||||
return this.clips;
|
||||
}
|
||||
|
||||
public void stopSound(String audioFileName) {
|
||||
Clip clip = activeClips.get(audioFileName);
|
||||
public void stopSound(long clipId) {
|
||||
Clip clip = activeClips.get(clipId);
|
||||
|
||||
if (clip == null) {
|
||||
return;
|
||||
@@ -90,7 +95,7 @@ public class SoundManager {
|
||||
|
||||
clip.stop();
|
||||
clip.close();
|
||||
activeClips.remove(audioFileName);
|
||||
activeClips.remove(clipId);
|
||||
}
|
||||
|
||||
public void stopAllSounds() {
|
||||
|
||||
@@ -8,5 +8,5 @@ public class AudioEvents extends EventsBase {
|
||||
public record PlayAudio(String fileNameNoExtensionAndNoDirectory, boolean loop)
|
||||
implements EventWithoutSnowflake {}
|
||||
|
||||
public record StopAudio(String fileNameNoExtensionAndNoDirectory) implements EventWithoutSnowflake {}
|
||||
public record StopAudio(long clipId) implements EventWithoutSnowflake {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user