mardi 30 décembre 2014

Cannot load sound later during runtime

Hello I have created little 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's working fine when all actors are created before game loop but when I shoot a bullet and I want to play a 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 with that? Thank you.


Aucun commentaire:

Enregistrer un commentaire