网址的问题

时间:2014-05-23 17:37:00

标签: java url jar

我的音频文件路径有问题。

当我编译项目时,音频听起来不错,但是当我打开jar时,它给了我这个错误(我没有看到错误,但我看不出我做错了什么......):

很抱歉,如果它的西班牙文“No existe el archivo o el directorio”意味着“没有这样的文件或目录”。

/home/user1/NetBeansProjects/project1/dist/project1.jar!/music/2.wav 线程“Thread-0”中的异常java.lang.IllegalStateException:java.io.FileNotFoundException:/home/user1/NetBeansProjects/project1/dist/project1.jar!/ music / 2.wav(No existe el archivo o el directorio)     在logic.AudioFilePlayer.run(AudioFilePlayer.java:54) 引起:java.io.FileNotFoundException:/home/user1/NetBeansProjects/project1/dist/project1.jar!/ music / 2.wav(No existe el archivo o el directorio)     at java.io.FileInputStream.open(Native Method)     在java.io.FileInputStream。(FileInputStream.java:146)     at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(WaveFloatFileReader.java:164)     在javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1179)     在logic.AudioFilePlayer.run(AudioFilePlayer.java:36)

以下是代码:

boolean loop = true;
private final URL url = getClass().getResource("/music/2.wav");
private final String convertFilePath = url.toString();
String filePath = convertFilePath.substring(convertFilePath.lastIndexOf("file:") + 5);

@Override
public void run() {
    while (loop == true) {
        final File file = new File(filePath);
        System.out.println(filePath);
        try (final AudioInputStream in = getAudioInputStream(file)) {

            final AudioFormat outFormat = getOutFormat(in.getFormat());
            final Info info = new Info(SourceDataLine.class, outFormat);

            try (final SourceDataLine line
                    = (SourceDataLine) AudioSystem.getLine(info)) {

                if (line != null) {
                    line.open(outFormat);
                    line.start();
                    stream(getAudioInputStream(outFormat, in), line);
                    line.drain();
                    line.stop();
                }
            }

        } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
            throw new IllegalStateException(e);
        }
    }
}

private AudioFormat getOutFormat(AudioFormat inFormat) {
    final int ch = inFormat.getChannels();
    final float rate = inFormat.getSampleRate();
    return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}

private void stream(AudioInputStream in, SourceDataLine line)
        throws IOException {
    final byte[] buffer = new byte[4096];
    for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
        line.write(buffer, 0, n);
    }
}

1 个答案:

答案 0 :(得分:1)

使用JAR时,您应该处理jar:file网址,而不是操纵它并尝试自己创建File对象。如果它支持,您应该将从getResource获得的URL直接传递给构造函数。

// get the URL of the file as usual
URL url = getClass().getResource("/music/2.wav");

// get stream directly from URL, which could be a file or a jar:file
AudioInputStream in = getAudioInputStream(url);

如果这不起作用,并且您的库可以获取输入流,您可以尝试使用ClassLoader#getResourceAsStream并将其传递给适当的构造函数/方法。

否则,您可以尝试将文件解压缩到本地某处,然后传递该文件的路径。

我的猜测是,当给定一个URL(带有jar:file协议)时,某些库根本不支持从JAR加载文件。