2012-02-08 1 views
0

sun.audio API에서 .wav 파일이 지원되는 파일 임에도 불구하고 분명히 내가 갖고 있었던 파일이 아니어야합니다. .aiff 파일은 현재 작동하지만이 방법으로는 더 좋은 방법은 찾지 못했습니다.Java 프로그램에서 소리 재생 만들기

String strFilename = "C:\\Documents and Settings\\gkehoe\\Network\\GIM\\Explode.aiff"; 
    File soundFile = new File(strFilename); 

    AudioInputStream audioInputStream = null; 
    try 
    { 
     audioInputStream = AudioSystem.getAudioInputStream(soundFile); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    AudioFormat audioFormat = audioInputStream.getFormat(); 
    SourceDataLine line = null; 
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, 
              audioFormat); 
    try 
    { 
     line = (SourceDataLine) AudioSystem.getLine(info); 

     /* 
      The line is there, but it is not yet ready to 
      receive audio data. We have to open the line. 
     */ 
     line.open(audioFormat); 
    } 
    catch (LineUnavailableException e) 
    { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    line.start(); 

    int nBytesRead = 0; 
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; 
    while (nBytesRead != -1) 
    { 
     try 
     { 
      nBytesRead = audioInputStream.read(abData, 0, abData.length); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     if (nBytesRead >= 0) 
     { 
      int nBytesWritten = line.write(abData, 0, nBytesRead); 
     } 
    } 
    line.drain(); 

    /* 
     All data are played. We can close the shop. 
    */ 
    line.close(); 
+0

이 광고를 보았습니까 http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java#comment4884807_26311? – Mob

+0

네,하지만이 코드를 가지고있는 것은 제가 한 것입니다. 그것은 작동하지 않습니다. –

+0

질문이 있으십니까? –

답변

0

source code에 따르면이 지원되는 파일 형식으로 인식되지 않습니다.

+0

은 .wav가 지원되지 않습니까? 지원되는 유형 중 하나였습니다. –

+0

나는 그렇게 말하지 않았다. 그것은 단지 인정되지 않습니다. 다른 사람과 시도 했니? – teodozjan

0

Wav 파일이 지원되지만 많은 변수가 있으며 그 중 일부는 지원되지 않습니다. 예를 들어 wav가 44100 대신 48000으로 인코딩되거나 16 비트 인코딩 대신 24 또는 32 비트로 인코딩되는 경우 인식 할 수없는 형식 예외가 발생할 수 있습니다.

어떤 오류가 발생 했습니까?

wav 파일의 사양 (속성)은 무엇입니까?

Audacity와 같은 도구를 사용하여 하나의 wav에서 호환 가능한 wav로 변환 할 수 있습니다. 내가 wav 파일에 사용하는 포맷은 다음과 같은 속성이 있습니다

16-bit encoding 
little endian 
44100 sample rate 
stereo 

정말 코드 예제 자체를 자세히 보지 않았다. 나는 this 재생 예제를 좋아한다.