alexh
June 9, 2009, 3:41pm
1
Hi ich habe im Package “img” das File “music.wav” dieses Musikstück möchte ich nun laden und abspielen, endloslang.
Dazu verwende ich folgenden Code:
audioInputStream.read();```
Es klappt jedoch nicht. Was ist denn daran falsch?
Gruß Alex
Versuch’s mal so:
SourceDataLine dataLine;
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/img/music.wav"));
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
dataLine = (SourceDataLine) AudioSystem.getLine(info);
dataLine.open(audioFormat, 524288);// 128Kb
dataLine.start();
int readBytes = 0;
byte[] audioBuffer = new byte[524288];// 128Kb
try {
while (readBytes != -1) {
readBytes = audioInputStream.read(audioBuffer, 0, audioBuffer.length);
if (readBytes >= 0) {
dataLine.write(audioBuffer, 0, readBytes);
}
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
dataLine.drain();
dataLine.close();
}
} catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}```
alexh
June 10, 2009, 12:07am
3
Hallo AndreUhres,
ihre Lösung ist perfekt, alles klappt wunderbar. Aber ein Problem habe ich noch, ich lade einen Splashscreen(startbildschirm) und dann das lied, danach komtm das hauptmenü, das menü kommt, der ss geht aber nicht weg obwohl er das nach 3s machen soll, muss ich vllt mulltithreading einführn um das erst nach 2s oder so zu starten?
Gruß Alex