2015-12-23 62 views
0

我一直在試圖爲我的朋友做一個加載程序。他說,如果它播放音樂會更好,所以我試圖添加音樂,它說它不起作用。它不斷給我一個錯誤,說系統找不到指定的文件,但文件與類相同。JFrame加載音樂現在運行

static File sound; 
static boolean muted = false; 
static float volume = 100.0f; 
static float pan = 0.0f; 
static double seconds = 0.0d; 
static boolean loopedForever = false; 
static int loopTimes = 0; 
static int loopsDone = 0; 

public static void main(String[] args){ 
    sound = new File("src/196006__corsica-s__patriceo.wav"); 
    new Thread(play).start(); 
} 
final static Runnable play = new Runnable() // This Thread/Runnabe is for playing the sound 
{ 
    public void run() 
    { 
     try 
     { 
      // Check if the audio file is a .wav file 
      if (sound.getName().toLowerCase().contains(".wav")) 
      { 
       AudioInputStream stream = AudioSystem.getAudioInputStream(sound); 

       AudioFormat format = stream.getFormat(); 

       if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) 
       { 
        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
          format.getSampleRate(), 
          format.getSampleSizeInBits() * 2, 
          format.getChannels(), 
          format.getFrameSize() * 2, 
          format.getFrameRate(), 
          true); 

        stream = AudioSystem.getAudioInputStream(format, stream); 
       } 

       SourceDataLine.Info info = new DataLine.Info(
         SourceDataLine.class, 
         stream.getFormat(), 
         (int) (stream.getFrameLength() * format.getFrameSize())); 

       SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); 
       line.open(stream.getFormat()); 
       line.start(); 

       // Set Volume 
       FloatControl volume_control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 
       volume_control.setValue((float) (Math.log(volume/100.0f)/Math.log(10.0f) * 20.0f)); 

       // Mute 
       BooleanControl mute_control = (BooleanControl) line.getControl(BooleanControl.Type.MUTE); 
       mute_control.setValue(muted); 

       FloatControl pan_control = (FloatControl) line.getControl(FloatControl.Type.PAN); 
       pan_control.setValue(pan); 

       long last_update = System.currentTimeMillis(); 
       double since_last_update = (System.currentTimeMillis() - last_update)/1000.0d; 

       // Wait the amount of seconds set before continuing 
       while (since_last_update < seconds) 
       { 
        since_last_update = (System.currentTimeMillis() - last_update)/1000.0d; 
       } 

       System.out.println("Playing!"); 

       int num_read = 0; 
       byte[] buf = new byte[line.getBufferSize()]; 

       while ((num_read = stream.read(buf, 0, buf.length)) >= 0) 
       { 
        int offset = 0; 

        while (offset < num_read) 
        { 
         offset += line.write(buf, offset, num_read - offset); 
        } 
       } 

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

       if (loopedForever) 
       { 
        new Thread(play).start(); 
       } 
       else if (loopsDone < loopTimes) 
       { 
        loopsDone++; 
        new Thread(play).start(); 
       } 
      } 
     } 
     catch (Exception ex) {ex.printStackTrace();} 

    } 
}; 
+0

答案可能是在這裏:http://stackoverflow.com/questions/16570523/getresourceasstream -returns-null –

+0

嘗試將文件移出包並移入源文件夾。如果您在加載文件時遇到麻煩,請不要粘貼太多的代碼,10條相關的代碼綽綽有餘。 – user1803551

+0

您有兩種方式試圖訪問.wav文件:'sound = new File(「196006__corsica-s__patriceo.wav」);'和InputStream is = getClass()。getClassLoader()。getResourceAsStream(「196006_corsica- s_patriceo.wav「);'哪一行拋出錯誤? –

回答

1

我測試了你的代碼,它工作正常。問題在於文件夾/包層次結構中文件的位置。

這是我如何訪問基於你的代碼的文件:

public class Example { 

    public static void main(String[] args) { 

     sound = new File("harpsi-cs.wav"); 
     new Thread(play).start(); 
    } 
} 

,這是我的層次:

enter image description here

經常項目創建一個資源文件夾,把他們的文件存在。在這種情況下,不要忘了讓一個源文件夾,並相應地改變你的路徑:

sound = new File("resources/harpsi-cs.wav"); 

enter image description here

+0

當您運行代碼時,您是否更改了格式,因爲當我嘗試運行它時,它給了我這個錯誤: 'java.lang.IllegalArgumentException :無行匹配接口SourceDataLine支持格式PCM_SIGNED支持96000.0 Hz,24位,單聲道,3字節/幀,little-endian和1984563至1984563字節的緩衝區.' – Gray

+0

@Gray更改了什麼格式?該文件是一個'.wav'文件。 – user1803551

+0

@Gray下面是我使用的文件:https://ccrma.stanford.edu/~jos/wav/harpsi-cs.wav – user1803551