Ich versuche, den Code zu verwenden, der verfügbar ist auf: How can I play sound in Java? aber ich kann Frage nicht dort, da dies ein neues Konto ist und nur 1 Ruf haben.Was ist falsch an meinem Code, so dass es keine WAV-Datei abspielen kann?
Original-Code:
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();
}
und dies ist mein Code:
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");
}
}
Wenn ich den Code ausführen i empfangen "null" als Ausgabe und es kam kein Ton heraus. Ich habe den Dateinamen und den Pfad überprüft, es ist korrekt.
screenshots:
Vielen Dank im Voraus.
Versuch 'AudioInputStream input = AudioSystem.getAudioInputStream (Main.class.getResourceAsStream (url))' – keety
versuchte es, keinen Unterschied, thx für die Hilfe trotzdem:) –
Bearbeitete meine Antwort, siehe unten –