2013-08-20 3 views
0

나는 오디오를 재생할 게임이 있습니다. 내가 할 수있는 일은 다음과 같은 인수로 ClipPlayer 개체를 선언 할 수 있어야한다는 것입니다. 경로 이름. 그런 다음 생성자가 사운드를로드 할 수 있어야합니다. 그런 다음 해당 오브젝트를 호출하여 지연을 피할 수 있습니다. 이것은 현재 사운드를 위해 사용하고있는 코드입니다.게임에서 지연없이 java에서 .wav 파일을 재생하는 방법

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class ClipPlayer { 

    AudioInputStream in; 

    AudioFormat decodedFormat; 

    AudioInputStream din; 

    AudioFormat baseFormat; 

    SourceDataLine line; 

    private boolean loop; 

    private BufferedInputStream stream; 

    // private ByteArrayInputStream stream; 

    /** 
    * recreate the stream 
    * 
    */ 
    public void reset() { 
     try { 
      stream.reset(); 
      in = AudioSystem.getAudioInputStream(stream); 
      din = AudioSystem.getAudioInputStream(decodedFormat, in); 
      line = getLine(decodedFormat); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void close() { 
     try { 
      line.close(); 
      din.close(); 
      in.close(); 
     } catch (IOException e) { 
     } 
    } 

    ClipPlayer(String filename, boolean loop) { 
     this(filename); 
     this.loop = loop; 
    } 

    ClipPlayer(String filename) { 
     this.loop = false; 
     try { 
      InputStream raw = Object.class.getResourceAsStream(filename); 
      stream = new BufferedInputStream(raw); 

      // ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      // byte[] buffer = new byte[1024]; 
      // int read = raw.read(buffer); 
      // while(read > 0) { 
      // out.write(buffer, 0, read); 
      // read = raw.read(buffer); 
      // } 
      // stream = new ByteArrayInputStream(out.toByteArray()); 

      in = AudioSystem.getAudioInputStream(stream); 
      din = null; 

      if (in != null) { 
       baseFormat = in.getFormat(); 

       decodedFormat = new AudioFormat(
         AudioFormat.Encoding.PCM_SIGNED, baseFormat 
           .getSampleRate(), 16, baseFormat.getChannels(), 
         baseFormat.getChannels() * 2, baseFormat 
           .getSampleRate(), false); 

       din = AudioSystem.getAudioInputStream(decodedFormat, in); 
       line = getLine(decodedFormat); 
      } 
     } catch (UnsupportedAudioFileException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (LineUnavailableException e) { 
      e.printStackTrace(); 
     } 
    } 

    private SourceDataLine getLine(AudioFormat audioFormat) 
      throws LineUnavailableException { 
     SourceDataLine res = null; 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, 
       audioFormat); 
     res = (SourceDataLine) AudioSystem.getLine(info); 
     res.open(audioFormat); 
     return res; 
    } 

    public void play() { 

     try { 
      boolean firstTime = true; 
      while (firstTime || loop) { 

       firstTime = false; 
       byte[] data = new byte[4096]; 

       if (line != null) { 

        line.start(); 
        int nBytesRead = 0; 

        while (nBytesRead != -1) { 
         nBytesRead = din.read(data, 0, data.length); 
         if (nBytesRead != -1) 
          line.write(data, 0, nBytesRead); 
        } 

        line.drain(); 
        line.stop(); 
        line.close(); 

        reset(); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

답변

1

사운드를 클래스 속성으로 선언하고로드하고 캐시합니다. 그런 다음 듣고 자 할 때 Clip에서 재생하십시오.

+0

나는 전에 자바 오디오로 일한 적이 없으므로 그렇게하기가 힘듭니다. 아마도 그렇게 할 수있는 코드를 줄 수 있습니까? –

+2

몇 가지 좋은 예가 있습니다 (여기 [http://stackoverflow.com/tags/javasound/info]). –

관련 문제