Implement asynchronous playing of sounds.
This commit is contained in:
parent
2f6eddcb46
commit
0c270c8665
|
@ -5,6 +5,7 @@ import java.awt.event.KeyEvent;
|
|||
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||
import de.teamteamteam.spacescooter.control.KeyboardListener;
|
||||
import de.teamteamteam.spacescooter.entity.shot.Shot;
|
||||
import de.teamteamteam.spacescooter.sound.SoundSystem;
|
||||
import de.teamteamteam.spacescooter.utility.GameConfig;
|
||||
|
||||
public class Player extends ShootingEntity implements KeyboardListener {
|
||||
|
@ -56,6 +57,11 @@ public class Player extends ShootingEntity implements KeyboardListener {
|
|||
this.canMove = canMove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void explode() {
|
||||
super.explode();
|
||||
SoundSystem.playSound("sounds/abgang.wav");
|
||||
}
|
||||
|
||||
/**
|
||||
* On cleanup, unregister from the keyboard.
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package de.teamteamteam.spacescooter.sound;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.BooleanControl;
|
||||
|
@ -11,6 +14,9 @@ import javax.sound.sampled.Line;
|
|||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
import de.teamteamteam.spacescooter.utility.Loader;
|
||||
|
||||
|
||||
public class SoundSystem {
|
||||
|
@ -75,22 +81,32 @@ public class SoundSystem {
|
|||
/**
|
||||
* Create a SourceDataLine and play the BufferedInputStream into it.
|
||||
*/
|
||||
public static void playFromInputStream(BufferedInputStream inputStream) {
|
||||
try {
|
||||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream);
|
||||
SoundSystem.sourceDataLine = AudioSystem.getSourceDataLine(audioInputStream.getFormat());
|
||||
SoundSystem.sourceDataLine.open(audioInputStream.getFormat());
|
||||
SoundSystem.sourceDataLine.start();
|
||||
inputStream.reset();
|
||||
byte[] b = new byte[512];
|
||||
while (inputStream.available() > 0) {
|
||||
inputStream.read(b, 0, 512);
|
||||
SoundSystem.sourceDataLine.write(b, 0, 512);
|
||||
public static void playFromAudioInputStream(URL soundURL) {
|
||||
final URL fSoundURL = soundURL;
|
||||
Thread soundThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
AudioInputStream sound = SoundSystem.getAudioInputStreamByURL(fSoundURL);
|
||||
sound.reset();
|
||||
SoundSystem.sourceDataLine = AudioSystem.getSourceDataLine(sound.getFormat());
|
||||
SoundSystem.sourceDataLine.open(sound.getFormat());
|
||||
SoundSystem.sourceDataLine.start();
|
||||
sound.reset();
|
||||
byte[] b = new byte[512];
|
||||
while (sound.available() > 0) {
|
||||
sound.read(b, 0, 512);
|
||||
SoundSystem.sourceDataLine.write(b, 0, 512);
|
||||
}
|
||||
SoundSystem.sourceDataLine.drain();
|
||||
SoundSystem.sourceDataLine.close();
|
||||
sound.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
SoundSystem.sourceDataLine.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
soundThread.setName("Sound: " + soundURL.toString());
|
||||
soundThread.start();
|
||||
}
|
||||
|
||||
public static void setVolume(float volume) {
|
||||
|
@ -109,4 +125,14 @@ public class SoundSystem {
|
|||
BooleanControl bc = (BooleanControl) subC[1];
|
||||
bc.setValue(false);
|
||||
}
|
||||
|
||||
|
||||
public static void playSound(String filename) {
|
||||
SoundSystem.playFromAudioInputStream(Loader.getAudioInputStreamByFilename(filename));
|
||||
}
|
||||
|
||||
|
||||
public static AudioInputStream getAudioInputStreamByURL(URL soundURL) throws UnsupportedAudioFileException, IOException {
|
||||
return AudioSystem.getAudioInputStream(new BufferedInputStream(soundURL.openStream()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
package de.teamteamteam.spacescooter.utility;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
import de.teamteamteam.spacescooter.screen.LoadingScreen;
|
||||
import de.teamteamteam.spacescooter.sound.SoundSystem;
|
||||
|
||||
/**
|
||||
* This Loader prefetches all required resources for the Game, such as images or
|
||||
|
@ -25,13 +25,13 @@ public class Loader {
|
|||
private static Hashtable<String, BufferedImage> images;
|
||||
|
||||
/**
|
||||
* HashTable containing loaded AudioInputStreams
|
||||
* HashTable containing the loaded sounds URLs
|
||||
*/
|
||||
private static Hashtable<String, AudioInputStream> sounds;
|
||||
private static Hashtable<String, URL> sounds;
|
||||
|
||||
static {
|
||||
Loader.images = new Hashtable<String, BufferedImage>();
|
||||
Loader.sounds = new Hashtable<String, AudioInputStream>();
|
||||
Loader.sounds = new Hashtable<String, URL>();
|
||||
}
|
||||
|
||||
|
||||
|
@ -55,7 +55,7 @@ public class Loader {
|
|||
/**
|
||||
* Return the loaded AudioInputStream by its relative filename.
|
||||
*/
|
||||
public static AudioInputStream getAudioInputStreamByFilename(String filename) {
|
||||
public static URL getAudioInputStreamByFilename(String filename) {
|
||||
if(CodeEnvironment.isJar()) {
|
||||
return Loader.sounds.get(filename);
|
||||
} else {
|
||||
|
@ -107,8 +107,11 @@ public class Loader {
|
|||
*/
|
||||
private static void addAudioInputStreamByFilename(String filename) {
|
||||
try {
|
||||
AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(Loader.class.getClassLoader().getResourceAsStream(filename)));
|
||||
Loader.sounds.put(filename, sound);
|
||||
URL soundURL = Loader.class.getClassLoader().getResource(filename);
|
||||
//make sure the sound is in a valid AudioFormat
|
||||
AudioInputStream sound = SoundSystem.getAudioInputStreamByURL(soundURL);
|
||||
sound.close();
|
||||
Loader.sounds.put(filename, soundURL);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Unable to load AudioInputStream: " + filename);
|
||||
e.printStackTrace();
|
||||
|
@ -116,7 +119,6 @@ public class Loader {
|
|||
System.err.println("Unsupported AudioFormat in file: " + filename);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue