B
burgfried
Gast
Vorweg: Dies ist mein erstes Thema in diesem Forum. Falls ich daher irgendeine Information übersehen habe, die mir helfen kann, weist mich darauf bitte hin. Das gleiche gilt für etwaige Verstöße gegen Regeln.
ich programmiere immer wieder ein wenig an einem kleinen spiel, wenn ich zeit habe. und dabei will ich auch sounds einbauen. ich habe dafür folgende klasse geschrieben, die mir eine gegebene sounddatei lädt und abspielt:
Der aufruf des abspielens erfolgt folgendermaßen.
aus dem spiel heraus kommt es oftmals dazu, dass der aufruf schnell hintereinander erfolgt.
mein problem ist, dass es manchmal vorkommt, dass überhaupt kein sound abgespielt wird. ich habe bis jetzt noch nicht herausgefunden, was die ursache dafür ist, da ich kein muster dahinter sehe.
vielleicht hat jemand hier mehr erfahrung auf diesem gebiet und kann mir etwas weiterhelfen.
danke.
ich programmiere immer wieder ein wenig an einem kleinen spiel, wenn ich zeit habe. und dabei will ich auch sounds einbauen. ich habe dafür folgende klasse geschrieben, die mir eine gegebene sounddatei lädt und abspielt:
Code:
import java.io.*;
import javax.sound.sampled.*;
public class BrickSound implements Runnable{
private Clip m_pBrickSound;
//loads the sound clip
public BrickSound() {
//loading the audio clips
try {
m_pBrickSound = openAudioClip(new File("wav/brick.wav"));
}
catch (Exception e) {
e.printStackTrace();
}
}
// method for the Runnable interface: plays the clip
public void run() {
if (m_pBrickSound == null) {
System.out.println("Help - no sound!");
return;
}
//play sound
m_pBrickSound.setFramePosition(0);
m_pBrickSound.start();
while (true) {
try {
Thread.sleep(100);
}
catch (InterruptedException ie) {
m_pBrickSound.stop();
return;
//ie.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
if (!m_pBrickSound.isRunning()) {
break;
}
}
m_pBrickSound.stop();
System.out.println("end reached");
}
//private method for loading the audio file into the member variable (copy paste from a tutorial)
private Clip openAudioClip(File pFile) throws Exception {
AudioInputStream pAudioInputStream = AudioSystem.getAudioInputStream(pFile);
AudioFormat pFormat = pAudioInputStream.getFormat();
//ALAW/ULAW samples in PCM konvertieren
if ((pFormat.getEncoding() == AudioFormat.Encoding.ULAW) ||
(pFormat.getEncoding() == AudioFormat.Encoding.ALAW)) {
AudioFormat pHelpFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
pFormat.getSampleRate(),
pFormat.getSampleSizeInBits() * 2,
pFormat.getChannels(),
pFormat.getFrameSize() * 2,
pFormat.getFrameRate(),
true);
pAudioInputStream = AudioSystem.getAudioInputStream(pHelpFormat, pAudioInputStream);
pFormat = pHelpFormat;
}
//Clip erzeugen und öffnen
DataLine.Info pInfo = new DataLine.Info(Clip.class, pFormat,
((int) pAudioInputStream.getFrameLength() * pFormat.getFrameSize()));
Clip pClip = (Clip)AudioSystem.getLine(pInfo);
pClip.open(pAudioInputStream);
return pClip;
}
}
Der aufruf des abspielens erfolgt folgendermaßen.
Code:
//init
BrickSound m_pBrickSound = new BrickSound();
Thread m_pBrickSoundThread = new Thread(m_pBrickSound);
...
//the sound should play now
if (m_pBrickSoundThread.isAlive()) {
m_pBrickSoundThread.interrupt();
}
m_pBrickSoundThread = new Thread(m_pBrickSound);
m_pBrickSoundThread.start();
aus dem spiel heraus kommt es oftmals dazu, dass der aufruf schnell hintereinander erfolgt.
mein problem ist, dass es manchmal vorkommt, dass überhaupt kein sound abgespielt wird. ich habe bis jetzt noch nicht herausgefunden, was die ursache dafür ist, da ich kein muster dahinter sehe.
vielleicht hat jemand hier mehr erfahrung auf diesem gebiet und kann mir etwas weiterhelfen.
danke.