2012-01-19 6 views
0

5 개의 wav 파일이 있습니다. sourceDataLine을 사용하여 단일 Java 프로그램에서 순차적으로 재생하려고합니다. 그러나 제 프로그램은 올바른 순서를 유지하지 못합니다. 누구나 나를 코드 세그먼트를 제공 할 수 있습니까?자바에서 사운드 재생

+7

번호가 우리를보기 ** 당신의 ** 코드를. –

+0

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 5 사운드 샘플이 아닌 2 사운드 샘플로 만들지 만, –

답변

3

Documentation을 확인 했습니까?

here에서이 예제를 시도 :

import java.io.*; 
import javax.sound.sampled.*; 
/** 
* Use SourceDataLine to read line-by-line from the external sound file.  
*/ 
public class SoundLineTest { 
    public static void main(String[] args) { 
     SourceDataLine soundLine = null; 
     int BUFFER_SIZE = 64*1024; // 64 KB 

     // Set up an audio input stream piped from the sound file. 
     try { 
     File soundFile = new File("gameover.wav"); 
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile); 
     AudioFormat audioFormat = audioInputStream.getFormat(); 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); 
     soundLine = (SourceDataLine) AudioSystem.getLine(info); 
     soundLine.open(audioFormat); 
     soundLine.start(); 
     int nBytesRead = 0; 
     byte[] sampledData = new byte[BUFFER_SIZE]; 
     while (nBytesRead != -1) { 
      nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length); 
      if (nBytesRead >= 0) { 
       // Writes audio data to the mixer via this source data line. 
       soundLine.write(sampledData, 0, nBytesRead); 
      } 
     } 
     } catch (UnsupportedAudioFileException ex) { 
     ex.printStackTrace(); 
     } catch (IOException ex) { 
     ex.printStackTrace(); 
     } catch (LineUnavailableException ex) { 
     ex.printStackTrace(); 
     } finally { 
     soundLine.drain(); 
     soundLine.close(); 
     } 
    } 
} 
+1

이것은 기껏해야 의견입니다. :-) – Scorpion

+0

@Scorpio : 아직 끝나지 않았습니다. – CloudyMarble

+0

+1 우수한 편집. –