Unable to load sound later while running
Hi, I have created a small class to play sounds in my game. Here:
package sk.tuke.oop.game.sounds;
import java.applet.AudioClip;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
public class Sound implements AudioClip {
private Clip clip;
private FloatControl volume;
private int framePosition;
public Sound(String path) {
loadMusic(path);
}
public void loadMusic(String path) {
if (clip != null)
clip.stop();
clip = null;
if (!path.equals("")) {
File soundFile = null;
try {
soundFile = new File(path);
} catch (Exception e) {
return;
}
try {
AudioInputStream input = AudioSystem.getAudioInputStream(soundFile);
clip = AudioSystem.getClip();
clip.open(input);
} catch (Exception e) {
e.printStackTrace();
clip = null;
}
}
}
public void play() {
if (clip != null) {
stop();
clip.start();
}
}
public void stop() {
if (clip != null) {
clip.stop();
clip.setFramePosition(0);
}
}
public void pause() {
if (clip != null) {
if (clip.isRunning()) {
framePosition = clip.getFramePosition();
clip.stop();
}
}
}
public void unpause() {
if (clip != null) {
if (!clip.isRunning()) {
clip.setFramePosition(framePosition);
clip.start();
}
}
}
public void loop() {
if (clip != null) {
clip.loop(clip.LOOP_CONTINUOUSLY);
}
}
public void setVolume(float vol) {
if (volume.getMinimum()+ vol <= volume.getMaximum()) {
volume.setValue(volume.getMinimum());
volume.setValue(volume.getValue() + vol);
}
}
}
It works fine when all the actors are created before the game loop, but when I shoot the bullet and I want to play the sound I get:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
could you help me? Thank.
0
source to share