我正在使用一個聲音播放器,我試圖創建一個while循環,知道何時終止它正在播放JFrame
。while循環應該循環,而剪輯(記錄)是播放並在剪輯完成時結束。但是,這是一個交易:while循環只在打印出某些東西時才起作用,我不希望它做什麼?雖然循環不工作在音頻播放器
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
public class SoundClipTest extends JFrame {
public SoundClipTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
// Open an audio input stream.
File soundFile = new File("Testing/recording (2).wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
//URL url = new URL("http://maximumdonline.com/miscwavs/pacman.wav");
//AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
this.setVisible(true);
this.setVisible(false);
boolean run = true;
//problem While Loop
while(clip.isActive()){
//Only works when I print something out during the loop:
//System.out.println(run);
}
//Test While Loop has ended.
run = false;
System.out.println(run);
//Close JFrame
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
注意:大多數的這種聲音代碼來自 https://www.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html 它的工作原理。我已經測試過,但是我的while循環正在哭泣!
Gieva看看:http://stackoverflow.com/questions/557903/how-can-我等待換一個Java的聲音剪輯到完成扮演後衛 – Linuslabo