2011-06-17 168 views
0

如何在到達結束時播放* .wav循環?如何在Java中播放* .wav循環?

我的代碼如下所示:

public class SoundPlayer implements Runnable{ 
public SoundPlayer(String filename){ 
    is=Main.class.getResourceAsStream("sounds/"+filename); 
} 
@Override 
public void run() { 
    // TODO Auto-generated method stub 
    playSound(); 
} 

public void playSound(){ 
    try { 
     audioStream = AudioSystem.getAudioInputStream(is); 
    } catch (Exception e){ 
     e.printStackTrace(); 
     System.exit(1); 
    } 

    audioFormat = audioStream.getFormat(); 

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); 
    try { 
     sourceLine = (SourceDataLine) AudioSystem.getLine(info); 
     sourceLine.open(audioFormat); 
    } catch (LineUnavailableException e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } 

    sourceLine.start(); 

    int nBytesRead = 0; 
    int bufferSize = audioFormat.getFrameSize() * 
    Math.round(audioFormat.getSampleRate()/10); 
    byte[] abData = new byte[bufferSize]; 
    while (nBytesRead != -1) { 
     try { 
      nBytesRead = audioStream.read(abData, 0, abData.length); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (nBytesRead >= 0) { 
      @SuppressWarnings("unused") 
      int nBytesWritten = sourceLine.write(abData, 0, nBytesRead); 
     } 
     else { 
      audioStream.reset(); //I add this code but didn't effect 
     } 
    } 

    sourceLine.drain(); 
    sourceLine.close(); 
} 


private final int BUFFER_SIZE = 128000; 
private File soundFile; 
private AudioInputStream audioStream; 
private AudioFormat audioFormat; 
private SourceDataLine sourceLine; 
private InputStream is; 

請幫助我。

+1

你有什麼錯誤嗎?它似乎在哪裏失敗? etc ... – RMT

+0

它沒有得到任何錯誤。但是,該代碼沒有循環* .wav文件到達結束時:( – Kenjiro

回答

0

如果是短聲樣品,請使用Clip。使用loop(int)方法設置要重複多少次。

請參閱an example

+0

如何設置無限循環?以及如何停止當我完成嗎?因爲這是背景音樂的遊戲,所以我不知道究竟有多少循環我需要:( – Kenjiro

+0

@Kenjiro:「如何設置無限循環?」1)按照我的JavaDocs提供的'loop(count)'方法的鏈接2)讀取它們,特別是單個參數的描述'count',以及可能的值是什麼。 –

+0

@鑑天郎:「我完成後怎麼停下來?」您可以嘗試閱讀這些細節手冊。 –

1

我創建了這個類,在單獨的線程中播放.wav聲音。隨意使用和適應,如果你想:

import java.io.*; 
import javax.sound.sampled.*; 
public class CPSound implements Runnable 
{ 
     String fileLocation = "alarm.wav"; 
     CPSound() 
     { 

     } 
     public void play(String fileName) 
     { 
       Thread t = new Thread(this); 
       fileLocation = fileName; 
       t.start(); 
     } 
    public void run() 
    { 
     playSound(fileLocation); 
    } 
     private void playSound(String fileName) 
     { 
       File soundFile = new File(fileName); 
       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); 
         line.open(audioFormat); 
       } 
       catch (LineUnavailableException e) 
       { 
         e.printStackTrace(); 
       } 
       catch (Exception e) 
       { 
         e.printStackTrace(); 
       } 
       line.start(); 
       int  nBytesRead = 0; 
       byte[] abData = new byte[128000]; 
       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(); 
       line.close(); 
     } 
} 

乾杯!

1
import java.io.File; 

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

public class Sound_player implements Runnable { 
    private static boolean play_music = false; 
    private AudioInputStream audioInputStream = null; 
    private File soundFile; 
    private static String filename; 
    private int EXTERNAL_BUFFER_SIZE = 2048; 

    public void run(){ 
     while(true){ 
      initialize(); 
     } 
    } 

    public void setFile(String file){ 
     filename = file; 
    } 

    private boolean initialize(){ 

     soundFile = new File(filename); 

     if(!soundFile.exists()){ 
      System.err.println("Wav file not found: " + filename); 
      return false; 
     } 


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

     AudioFormat format = audioInputStream.getFormat(); 

     SourceDataLine auline = null; 

     // describe a desired line 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 

     try 
     { 
      auline = (SourceDataLine) AudioSystem.getLine(info); 

      auline.open(); 
     } catch (Exception e){ 
      System.err.println(e); 
      return false; 
     } 

     // Allows line to engage in data i/o 
     auline.start(); 
     int nBytesRead = 0; 
     byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; 
     try 
     { 
      while(nBytesRead != -1){ 
       nBytesRead = audioInputStream.read(abData, 0, abData.length); 
       auline.write(abData, 0, nBytesRead); 
       } 
      } 
     } catch(Exception e){ 
      System.err.println(e); 
      return false; 
     } 
     finally 
     { 
      auline.drain(); 

      auline.close(); 
     } 
     return true; 
    } 
}