사용할 수있는 코드를 사용하려고합니다 : How can I play sound in Java? 하지만 새 계정이므로 평판이 1 만이므로 질문을 게시 할 수 없습니다.내 코드가 잘못되어 wav 파일을 재생할 수 없습니까?
원래 코드 :
public static synchronized void playSound(final String url) {
new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
이 내 코드입니다 : 내가 코드를 실행하면
package sound_test;
import javax.sound.sampled.*;
public class Main {
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
public static void main(String[] args) {
// TODO code application logic here
playSound("C:\\warning_test.wav");
}
}
내가 출력으로 "널 (null)"수신하고 소리가 나왔다 없습니다. 파일 이름과 경로를 확인했는데 정확합니다.
스크린 샷 :
은 사전에 감사합니다.
시도'AudioInputStream를 inputStream을 = AudioSystem.getAudioInputStream (Main.class.getResourceAsStream (URL)가)' – keety
어쨌든, 도움을 들으 차이를 시도하지 :) –
내 대답을 편집했습니다. 아래를 참조하십시오. –