mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Added back changes from audio branch. byte[] is stored in base resource and different Resources can extract their data from it.
This commit is contained in:
@@ -5,51 +5,23 @@ import java.io.*;
|
||||
|
||||
public class AudioAsset extends BaseResource implements LoadableResource {
|
||||
|
||||
private AudioInputStream audioInputStream = null;
|
||||
private Clip clip = null;
|
||||
private boolean isLoaded = false;
|
||||
|
||||
|
||||
public AudioAsset(final File audioFile) {
|
||||
super(audioFile);
|
||||
}
|
||||
|
||||
public AudioInputStream getAudioStream() {
|
||||
return this.audioInputStream;
|
||||
}
|
||||
// Gets a new clip to play
|
||||
public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
this.audioInputStream = AudioSystem.getAudioInputStream(this.stream);
|
||||
// Get a new clip from audio system
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(this.audioInputStream);
|
||||
this.clip = clip;
|
||||
this.isLoaded = true;
|
||||
} catch (UnsupportedAudioFileException | LineUnavailableException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) { // TODO: Error handling
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Insert a new audio stream into the clip
|
||||
clip.open(this.getAudioStream());
|
||||
return clip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.clip.stop();
|
||||
this.clip.flush();
|
||||
this.clip.close();
|
||||
this.clip = null;
|
||||
this.isLoaded = false;
|
||||
// Generates a new audio stream from byte array
|
||||
private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
|
||||
return AudioSystem.getAudioInputStream(this.getInputStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoaded() {
|
||||
return this.isLoaded;
|
||||
}
|
||||
|
||||
public Clip getClip() {
|
||||
if (!this.isLoaded) this.load();
|
||||
return this.clip;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,27 +1,48 @@
|
||||
package org.toop.framework.asset.resources;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public abstract class BaseResource {
|
||||
|
||||
final InputStream stream;
|
||||
private byte[] rawData;
|
||||
final File file;
|
||||
private boolean isLoaded = false;
|
||||
|
||||
BaseResource(final File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return this.isLoaded;
|
||||
}
|
||||
|
||||
public void load() throws FileNotFoundException{
|
||||
try {
|
||||
this.stream = new BufferedInputStream(new FileInputStream(file));
|
||||
} catch (FileNotFoundException e) {
|
||||
this.rawData = Files.readAllBytes(file.toPath());
|
||||
isLoaded = true;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return this.stream;
|
||||
public void unload() {
|
||||
this.rawData = null;
|
||||
this.isLoaded = false;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
if (!isLoaded){
|
||||
try {
|
||||
this.load();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
public void load() throws FileNotFoundException {
|
||||
if (!this.isLoaded) {
|
||||
this.image = new Image(this.stream);
|
||||
this.image = new Image(this.getInputStream());
|
||||
this.isLoaded = true;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,11 @@ public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
if (!this.isLoaded) load();
|
||||
if (!this.isLoaded) try {
|
||||
load();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,11 @@ public class SoundManager {
|
||||
}
|
||||
|
||||
private void handlePlaySound(AudioEvents.PlayAudio event) {
|
||||
try {
|
||||
this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop());
|
||||
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStopSound(AudioEvents.StopAudio event) {
|
||||
@@ -45,7 +49,7 @@ public class SoundManager {
|
||||
this.audioResources.put(audioAsset.getName(), audioAsset.getResource());
|
||||
}
|
||||
|
||||
private long playSound(String audioFileName, boolean loop) {
|
||||
private long playSound(String audioFileName, boolean loop) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
|
||||
AudioAsset asset = audioResources.get(audioFileName);
|
||||
|
||||
// Return -1 which indicates resource wasn't available
|
||||
@@ -54,7 +58,7 @@ public class SoundManager {
|
||||
}
|
||||
|
||||
// Get a new clip from resource
|
||||
Clip clip = asset.getClip();
|
||||
Clip clip = asset.getNewClip();
|
||||
|
||||
// If supposed to loop make it loop, else just start it once
|
||||
if (loop) {
|
||||
|
||||
Reference in New Issue
Block a user