Sonido de explosión
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package net.h4ckx0r;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.sound.sampled.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
@@ -12,8 +14,12 @@ public class ResourceCache {
|
||||
public static final String[] IMAGEN_LADRILLO = new String[] {"LadrilloRojo.png", "LadrilloNaranja.png", "LadrilloAmarillo.png", "LadrilloVerde.png", "LadrilloAzul.png" };
|
||||
|
||||
|
||||
public static final String SONIDO_EXPLOSION = "explosion.wav";
|
||||
|
||||
private HashMap<String, BufferedImage> cacheImagenes = new HashMap<>();
|
||||
|
||||
private HashMap<String, Clip> cacheSonidos = new HashMap<>();
|
||||
|
||||
private static ResourceCache instance = null;
|
||||
|
||||
public static ResourceCache getInstance() {
|
||||
@@ -44,4 +50,39 @@ public class ResourceCache {
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
private Clip getSound(String nombreSonido) {
|
||||
Clip clip = cacheSonidos.get(nombreSonido);
|
||||
if (clip == null) {
|
||||
try {
|
||||
URL sonido = getClass().getResource("/sonidos/" + nombreSonido);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(sonido);
|
||||
clip = AudioSystem.getClip();
|
||||
clip.open(audioStream);
|
||||
cacheSonidos.put(nombreSonido, clip);
|
||||
} catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return clip;
|
||||
}
|
||||
|
||||
public void reproducirSonido(String sonido) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Clip clip = getSound(sonido);
|
||||
if (clip == null) return;
|
||||
|
||||
clip.setFramePosition(0);
|
||||
clip.start();
|
||||
|
||||
while (clip.isRunning()) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ public class Explosion extends Actor{
|
||||
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion8.png"));
|
||||
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion9.png"));
|
||||
this.sprite = this.spritesAnimacion.getFirst();
|
||||
|
||||
ResourceCache.getInstance().reproducirSonido(ResourceCache.SONIDO_EXPLOSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
BIN
src/main/resources/sonidos/explosion.wav
Normal file
BIN
src/main/resources/sonidos/explosion.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user