인터넷을 통해 wav 파일을 재생하는 방법을 살펴 보았습니다.하지만 별도의 스레드에서 재생되지 않기 때문에 노래가 끝날 때까지 프로그램이 중단되고 게임에서는 작동하지 않습니다. 그러나 별도의 스레드에서 모든 것을 재생하려고하면 지연됩니다. 누구든지이 문제를 해결할 수 있습니까?지연없이 wav 파일을 재생하는 방법은 무엇입니까?
참고 : 나는 또한 지속적으로 배경에 그림입니다 :
public class DrawThread extends Thread {
JFrame j;
public DrawThread(JFrame frame) {
j = frame;
}
public void run() {
while (!(isInterrupted())) {
if (j.isDisplayable() && j.isActive() && j.isEnabled())
j.update(j.getGraphics());
}
}
}
하고 업데이트하는 것은 그냥 페인트를 호출
public void paint(Graphics g) {
Graphics2D bg = (Graphics2D) g;
bg.setBackground(Color.black);
int w = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int h = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
if (time < 500) {
x[time] = r.nextInt(w) + 150;
y[time] = h;
z[time] = r.nextInt(w) + 150;
time++;
}
for (int i = 0; i < time; i++) {
y[i] -= forward;
x[i] -= right;
if (y[i] < 1) {
x[i] = r.nextInt(400) - 200;
y[i] = 300;
z[i] = r.nextInt(400) - 200;
} else if (y[i] > 300) {
x[i] = r.nextInt(400) - 200;
y[i] = 1;
z[i] = r.nextInt(400) - 200;
}
if (x[i] > 200)
x[i] = -200;
else if (x[i] < -200)
x[i] = 200;
bg.setColor(color);
bg.drawLine(
getWidth()/2 + (int) Math.round(x[i] * 200/y[i]),
getHeight()/2 + (int) Math.round(z[i] * 200/y[i]),
getWidth()
/2
+ (int) Math.round((x[i] + right) * 200
/Math.abs(y[i] + forward)),
getHeight()
/2
+ (int) Math.round((z[i]) * 200
/Math.abs(y[i] + forward)));
}
}
편집 : 내가 사용 음악 플레이어 클래스 (추가 나는) BigClip로 전환했습니다 당신은 코드를 표시하지 않은
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import org.crystalix.util.SoundListener;
public class MSHandler {
public static boolean pause = false;
public static boolean stop = false;
private static HashMap<String, BigClip> clips = new HashMap<String, BigClip>();
public static void stopAll() {
Collection<BigClip> c = clips.values();
for (BigClip b : c) {
System.out.println("Stopping "+b.toString());
b.stop();
System.out.println("Stopped "+b.toString());
}
}
public static void playMusic(File file) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
BigClip clip = new BigClip();
clip.open(audioIn);
clip.addLineListener(new SoundListener());
clip.loop(1);
clip.start();
clips.put(file.getName(), clip);
} catch (Exception e) {
System.err.println("Sound could not be started!");
e.printStackTrace(System.err);
}
}
}
1)'j.update (j.getGraphics());'하지 마세요. 다른 많은 문제를 일으킬 가능성이 있습니다. 2) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 3) [Java Sound tag Wiki] (http://stackoverflow.com/tags/javasound/info)에서 볼 수 있듯이 소리에 'Clip'을 사용하십시오. –
가능한 한 자주 페인트하려고하는 것이 어떻습니까? 그것은 그것이 반복적으로 페인트하게 만드는 유일한 방법입니다. 기본적으로 그렇게하지 않습니다. 또한 파일은 음악이 아니기 때문에 소리가 나지 않으므로 클립이 너무 크기 때문에 제대로 작동하지 않습니다. 실제로 Clip을 사용했는데 실패했습니다. –
* "가능한 한 자주 페인트하려고하는 것이 어떻습니까?"* 비디오 카드와 직접 인터페이스하는 언어를 사용하십시오. * ".. 너무 큽니다. 실제로 클립을 사용했지만 실패했습니다"*이 경우에는 ['BigClip'] (http://stackoverflow.com/a/5668510/418556)을 사용하십시오. –