从JAR执行时,项目不读取.wav文件

时间:2013-11-15 05:11:52

标签: java audio jar

用户通过单选按钮选择一个并按下播放按钮后播放声音片段的简单GUI应用程序。在清理和构建之后,从JAR文件执行会导致在选择剪辑并按下播放按钮时不播放声音。

条件:NetBeans IDE,声音在IDE中成功播放到包中,JAR中.wav文件的路径是正确的,文件在JAR中的正确目录中,使用2个类:一个用于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();
        } 

    } 
} 

2 个答案:

答案 0 :(得分:0)

我遇到了与此类似的问题,尝试使用与JAR文件内部相同的路径创建一个文件夹,但不在其中。为什么它工作我不知道,但我遇到了类似的问题,尝试使用与JAR文件内部相同的路径创建一个文件夹,但不在其中。为什么它有效我不知道,但这样做对我有用。希望这能解决你的问题。这样做对我有用。希望那个

答案 1 :(得分:0)

试试这段代码。

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

}