프로그램에 사운드를 추가하려고합니다. 프로그램에 사운드를 추가하는 방법에 대한 경험이 없습니다. 이 코드는 예외이지만 "null"을 지정하려고합니다. 나는 playSound 함수에서 어떤 인수를 전달해야하는지 모른다. 그래서 제발 도와주세요.프로그램에 사운드를 추가하는 방법은 무엇입니까?
import java.io.InputStream;
import sun.audio.*; //import the sun.audio package
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Main {
public Main() {
}
public static void main(String[] args) {
System.out.println("hello there");
playSound("Hi there");
}
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("pacman_chomp.wav"
+ url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
}
Null 어디서? 어느 선 이요? – OscarRyz
.wav 파일이나 다른 사운드 파일에 대한 경로를 지정해야합니다. –
@ console ... 두 번째 줄 "null"에 "Hello there"가 인쇄됩니다. – OOkhan