2013-11-15 32 views
0

在用戶選擇帶有單選按鈕並按下播放按鈕之後播放聲音片段的簡單GUI應用程序。清理並構建完成後,如果選擇剪輯並按下播放按鈕,則從JAR文件執行將不會播放聲音。當從JAR執行時,項目不讀取.wav文件

條件:NetBeans IDE聲音在IDE中成功播放到程序包中,JAR中.wav文件的路徑正確,文件位於正確目錄中的JAR中,使用2個類:一個用於GUI,另一個用於GUI .WAV處理類(在IDE中成功地都工作。在屏幕截圖的更多細節。

Project Directory

CMD output after load JAR and attempt to play sound

Directory of contents inside of the JAR using WINRAR to open

的代碼片段用於調用資源(能正常工作的IDE,所以沒有問題):

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
    if (jRadioButton1.isSelected()){ 
      URL file = QuotesButtonUI.class.getResource("/my/sounds/fear_converted.wav"); 
      new Player (file.getFile()).start(); 
    } 
    else if (jRadioButton2.isSelected()){ 

      URL file = QuotesButtonUI.class.getResource("/my/sounds/initiated_converted.wav"); 
      new Player (file.getFile()).start(); 
    } 

這是用來處理.WAV Player類。在GUI類中,我使用新的Player()。start()調用。我想在Player類中應該有一個getResource()調用,但我不知道。

package my.quotesbutton; 

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class Player extends Thread { 

    private String filename; 

    private Position curPosition; 

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

    enum Position { 
     LEFT, RIGHT, NORMAL 
    }; 

    public Player(String wavfile) { 
     filename = wavfile; 
     curPosition = Position.NORMAL; 
    } 

    public Player(String wavfile, Position p) { 
     filename = wavfile; 
     curPosition = p; 
    } 

    public void run() { 

     File soundFile = new File(filename); 
     if (!soundFile.exists()) { 
      System.err.println("Wave file not found: " + filename); 
      return; 
     } 

     AudioInputStream audioInputStream = null; 
     try { 
      audioInputStream = AudioSystem.getAudioInputStream(soundFile); 
     } catch (UnsupportedAudioFileException e1) { 
      e1.printStackTrace(); 
      return; 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
      return; 
     } 

     AudioFormat format = audioInputStream.getFormat(); 
     SourceDataLine auline = null; 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 

     try { 
      auline = (SourceDataLine) AudioSystem.getLine(info); 
      auline.open(format); 
     } catch (LineUnavailableException e) { 
      e.printStackTrace(); 
      return; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return; 
     } 

     if (auline.isControlSupported(FloatControl.Type.PAN)) { 
      FloatControl pan = (FloatControl) auline 
        .getControl(FloatControl.Type.PAN); 
      if (curPosition == Position.RIGHT) 
       pan.setValue(1.0f); 
      else if (curPosition == Position.LEFT) 
       pan.setValue(-1.0f); 
     } 

     auline.start(); 
     int nBytesRead = 0; 
     byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; 

     try { 
      while (nBytesRead != -1) { 
       nBytesRead = audioInputStream.read(abData, 0, abData.length); 
       if (nBytesRead >= 0) 
        auline.write(abData, 0, nBytesRead); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
     } finally { 
      auline.drain(); 
      auline.close(); 
     } 

    } 
} 

回答

0

我有類似這樣的問題,請嘗試將一個文件夾相同的路徑有您的JAR文件中,但它的外面。爲什麼它有效我不知道,但我遇到過類似問題,請嘗試製作與您在JAR文件內但在其外部具有相同路徑的文件夾。爲什麼它的作品我不知道,但這樣做對我有效。希望能解決你的問題。這樣做對我有效。希望

+0

我的意圖是讓我的辦公室裏的人打包成一個單獨的實體,不需要其他文件和依賴關係。這會工作嗎? – Joel

+0

試試傑里米的解決方案。 Mine會要求您在JAR中分發一個額外的文件夾。如果傑里米的解決方案對你有用,你是否介意在這裏發表評論,以便我知道並能夠調整我的計劃。謝謝 – Cameron

0

試試這個代碼。

try 
{ 
    InputStream in = QuotesButtonUI.class.getResourceAsStream("/my/sounds/fear_converted.wav"); 
    if (in != null) 
    { 
     //code to play sound here 
    } 
} 
catch (IOException ioe) 
{ 

} 
+0

因此,而不是.getFile(),你會用什麼方法來獲取流? – Joel

+0

看看https://blogs.oracle.com/geertjan/entry/how_to_create_a_movie – Jeremy

+0

不確定這是否合適,因爲我需要將資源用作文件。我的Player類在絕對路徑或IDE內工作正常。唯一的問題是一旦我將其構建到jar文件。否則,玩家根據需要/設計呈現聲音。 – Joel

相關問題