【问题标题】:Project not reading .wav files when executed from the JAR从 JAR 执行时项目不读取 .wav 文件
【发布时间】:2013-11-16 01:20:25
【问题描述】:

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

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

用于调用资源的代码 sn-p(这在 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 类中,我使用了 new 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 audio jar


    【解决方案1】:

    我遇到了与此类似的问题,请尝试创建一个与 JAR 文件内部路径相同但在其外部的文件夹。为什么它起作用我不知道,但我遇到了类似的问题,尝试创建一个与 JAR 文件内部路径相同但在其外部的文件夹。为什么它起作用我不知道,但这样做对我有用。希望这可以解决您的问题。这样做对我有用。希望

    【讨论】:

    • 我的目的是将这个打包成一个单独的实体提供给我办公室的人们,不需要其他文件和依赖项。这对它有用吗?
    • 试试 Jeremy 的解决方案。我的需要你有一个与 JAR 一起分发的附加文件夹。如果 Jeremy 的解决方案对您有用,您是否介意在此处评论它确实如此,我会知道并能够调整我的程序。谢谢
    【解决方案2】:

    试试这个代码。

    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 文件中。否则,播放器会根据需要/设计来渲染声音。
    • 嗯,您是“链接文件”还是“复制文件”到您的 netbeans java 项目中?
    猜你喜欢
    • 1970-01-01
    • 2020-02-27
    • 2023-02-23
    • 2020-07-31
    • 2010-12-06
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多