2012-04-13 3 views
1

사용할 수있는 코드를 사용하려고합니다 : 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)"수신하고 소리가 나왔다 없습니다. 파일 이름과 경로를 확인했는데 정확합니다.

스크린 샷 :

http://puu.sh/pkYo

http://puu.sh/pkZl

은 사전에 감사합니다.

+0

시도'AudioInputStream를 inputStream을 = AudioSystem.getAudioInputStream (Main.class.getResourceAsStream (URL)가)' – keety

+0

어쨌든, 도움을 들으 차이를 시도하지 :) –

+0

내 대답을 편집했습니다. 아래를 참조하십시오. –

답변

0

당신은

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

click.start(); i.e Thread.Sleep(4000);

후 지연을 추가 할 수 또는 당신은 당신이

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

가 작동하지 않습니다. 나 : ( 도움말에 대한 thx :) –

+0

@ chiaki_kei는 – keety

+0

대답했습니다 편집했습니다 대단히 감사합니다 :) –

0

코드를 완전히 복사 한 경우 유타 원래의 알았어, 당신이 그것을 전체 경로를 제공하기 때문에, 유 그냥 URL로 교체해야

path/to/sounds 

를 가리키는 :

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

편집 : 내가 여기에 노력하고 null를 가지고 잘. 내가 파일에서 audioInput을 만들 수를 변경 :

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); 
    } 
관련 문제