Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JAR执行时,项目未读取.wav文件_Java_Audio_Jar - Fatal编程技术网

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

Java 从JAR执行时,项目未读取.wav文件,java,audio,jar,Java,Audio,Jar,简单的GUI应用程序,用于在用户通过单选按钮选择声音片段并按下播放按钮后播放声音片段。在清理和构建之后,当选择剪辑并按下播放按钮时,从JAR文件执行将导致不播放声音 条件:NetBeans IDE,声音在指向包的IDE路径中成功播放,JAR中.wav文件的路径正确,JAR中的文件位于正确的目录中,使用2个类:一个用于GUI,一个用于.wav处理程序类(两者都在IDE中成功工作。更多详细信息见屏幕截图) 用于调用资源的代码段(在IDE中工作正常,因此没有问题): 这是用于处理.wav的Pla

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

条件:NetBeans IDE,声音在指向包的IDE路径中成功播放,JAR中.wav文件的路径正确,JAR中的文件位于正确的目录中,使用2个类:一个用于GUI,一个用于.wav处理程序类(两者都在IDE中成功工作。更多详细信息见屏幕截图)

用于调用资源的代码段(在IDE中工作正常,因此没有问题):

这是用于处理.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();
        } 

    } 
} 

我遇到过类似的问题,尝试创建一个与JAR文件内部路径相同但在其外部路径相同的文件夹。为什么工作我不知道但我遇到过类似的问题,尝试创建一个与JAR文件内部路径相同但在其外部路径相同的文件夹。为什么工作我不知道,但这样做对我有效。希望如此这很好地解决了您的问题。这样做对我来说很有效。希望您能试试这段代码

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

}

因此,不是.getFile(),您将使用什么方法获取流?不确定这是否合适,因为我需要将资源用作文件。我的Player类在这个项目中使用绝对路径或从IDE中工作正常。唯一的问题是一旦我将其构建为jar文件。否则,播放机将按照所需/设计呈现声音。uhmm“链接文件”或将“文件”复制到您的netbeans java项目中?我的目的是将此文件打包为一个不需要其他文件和依赖项的单一实体提供给我办公室的同事。这样做行吗?试试Jeremy的解决方案。我的解决方案要求您有一个随JAR一起分发的附加文件夹。如果Jeremy的解决方案对您有效,您介意与我们合作吗我在这里提醒说,我会知道,并能够调整我的程序。谢谢
try
{
    InputStream in = QuotesButtonUI.class.getResourceAsStream("/my/sounds/fear_converted.wav");
    if (in != null)
    {
        //code to play sound here
    }
}
catch (IOException ioe)
{

}