Java .wav声音播放器类中的getResource()

Java .wav声音播放器类中的getResource(),java,audio,executable-jar,Java,Audio,Executable Jar,简单的GUI应用程序,用于在用户通过单选按钮选择声音片段并按下播放按钮后播放声音片段。在清理和构建之后,当选择剪辑并按下播放按钮时,从JAR文件执行将导致不播放声音 条件:NetBeans IDE,声音在指向包的IDE路径中成功播放,JAR中.wav文件的路径正确,文件位于正确目录下的可执行JAR中,使用2个类:一个用于GUI,一个用于.wav处理程序类(两者都在IDE中成功工作。更多详细信息请参见屏幕截图。我认为在Player Cals中应该有一个getResource()方法调用,但我不知道

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

条件:NetBeans IDE,声音在指向包的IDE路径中成功播放,JAR中.wav文件的路径正确,文件位于正确目录下的可执行JAR中,使用2个类:一个用于GUI,一个用于.wav处理程序类(两者都在IDE中成功工作。更多详细信息请参见屏幕截图。我认为在Player Cals中应该有一个getResource()方法调用,但我不知道如何编写它

用于从GUI类内调用资源的代码段。基为new Player(“whateverthefilepathis”).start()(在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();
        } 

    } 
} 
您不能使用java.io.file API访问jar文件中的任何内容,因为jar中的项目不是文件

正如您所怀疑的,您确实需要使用getResource()方法(或getResourceAsStream()):

乍一看,这可能违反直觉,但getResource()可以处理文件和JAR(对于webapp,甚至可以处理远程资源,如链接教程中所示),类加载器将处理如何物理访问资源的脏细节。简言之:永远不要对资源使用文件API-应仅使用资源API访问资源

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();
        } 

    } 
}