我对 Java 中的音频有点陌生。我正在尝试做的是在播放音频时,我想重新绘制我的JComponent
,但SourceDataLine
阻止所有其他代码行,包括其他线程。这是我的play()
方法
public void play()
{
final AudioFormat af =new AudioFormat(Note.SAMPLE_RATE, 8, 1, true, true);
SourceDataLine line;
try {
line = AudioSystem.getSourceDataLine(af);
line.open(af, Note.SAMPLE_RATE);
line.start();
byte[] arr=data;
for(int position=0;position<arr.length;position++)
{
byte[] out={arr[position]};
line.write(out, 0, 1); //Blocks all user input (Mouse and Keyboard)
this.repaint(); //Need to repaint JComponent here
}
line.drain();
line.close();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}