如何在到達結束時播放* .wav循環?如何在Java中播放* .wav循環?
我的代碼如下所示:
public class SoundPlayer implements Runnable{
public SoundPlayer(String filename){
is=Main.class.getResourceAsStream("sounds/"+filename);
}
@Override
public void run() {
// TODO Auto-generated method stub
playSound();
}
public void playSound(){
try {
audioStream = AudioSystem.getAudioInputStream(is);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
int bufferSize = audioFormat.getFrameSize() *
Math.round(audioFormat.getSampleRate()/10);
byte[] abData = new byte[bufferSize];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
else {
audioStream.reset(); //I add this code but didn't effect
}
}
sourceLine.drain();
sourceLine.close();
}
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
private InputStream is;
請幫助我。
你有什麼錯誤嗎?它似乎在哪裏失敗? etc ... – RMT
它沒有得到任何錯誤。但是,該代碼沒有循環* .wav文件到達結束時:( – Kenjiro