2012-04-13 4 views
1

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:

http://puu.sh/pkYo

http://puu.sh/pkZl

Vielen Dank im Voraus.

+0

Versuch 'AudioInputStream input = AudioSystem.getAudioInputStream (Main.class.getResourceAsStream (url))' – keety

+0

versuchte es, keinen Unterschied, thx für die Hilfe trotzdem:) –

+0

Bearbeitete meine Antwort, siehe unten –

Antwort

0

Sie tun können,

AudioInputStream inputStream=AudioSystem.getAudioInputStream(new File(url)); 

fügen auch eine Verzögerung nach click.start(); i.e Thread.Sleep(4000);

oder wenn Sie wollen sicherstellen, dass es das gesamte Audio-Sample spielt Sie einen einfachen Code-Schnipsel wie

verwenden könnten
import javax.sound.sampled.*; 
import java.io.File; 

public class Main implements LineListener { 
private boolean done = false; 
public void update(LineEvent event) { 
    if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE) { 
     done = true; 
    } 
} 

public void waitonfinish() throws InterruptedException { 
    while(!done) { 
     Thread.sleep(1000); 
    } 
} 
public static void playSound(final String url) { 

    try { 
     Clip clip = AudioSystem.getClip(); 
     AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url)); 
     Main control = new Main(); 
     clip.addLineListener(control); 
     clip.open(inputStream); 
     clip.start(); 
     control.waitonfinish(); 

    } catch (Exception e) { 
     System.err.println(e.getMessage()); 
    } 
    } 

public static void main(String[] args) { 
    // TODO code application logic here 
    playSound("C:\\warning_test.wav"); 
} 
} 

`

+0

funktioniert nicht für ich :( thx für die Hilfe btw :) –

+0

@chiaki_kei hat die Antwort bearbeitet – keety

+0

es funktioniert vielen Dank :) –

0

Sie haben den Code komplett mito kopiert ut die im Original zu bemerken, verweist er auf

path/to/sounds 

, da Sie es den vollständigen Pfad geben, u es mit nur url ersetzen sollte:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream(url)); 

EDIT: ich hier versucht und bekam null als Gut. ich es änderte den Audioeingang aus einer Datei zu erstellen:

import java.io.File; 

import javax.sound.sampled.*; 

public class Main { 

    public static synchronized void playSound(final File file) { 
     new Thread(new Runnable() { 
      public void run() { 
       try { 
        Clip clip = AudioSystem.getClip(); 
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(file); 
        clip.open(inputStream); 
        clip.start(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
        System.err.println(e.getMessage()); 
       } 
      } 
     }).start(); 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     File file = new File("C:\\warning_test.wav"); 
     playSound(file); 
    }