java - 在GUI应用程序中使用getResourceAsStream

标签 java swing audio

我有一个字符引号应用程序,其中GUI类调用处理.wav文件的播放器类。用户使用单选按钮选择报价,然后单击播放按钮以启动。当.wav的路径定义为系统上的绝对路径时,该应用程序运行正常。我想将.wav文件合并到包my.sounds中,作为“/my/sounds/anyfilename.wav”,我知道我需要使用getResourceAsStream()方法,但是我不知道如何将其合并到GUI中调用Player类时的类。同样,Player类可以在绝对路径下正常工作。该错误是找不到文件错误。

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (jRadioButton1.isSelected()){
            new Player("/my/sounds/fear_converted.wav").start();
    }
    else if (jRadioButton2.isSelected()){

            new Player("C:/Users/joel.ramsey/Desktop/Audio for Quotes Program/initiated_converted.wav").start();
    }
    else if (jRadioButton3.isSelected()){

            new Player("C:/Users/joel.ramsey/Desktop/Audio for Quotes Program/fight_converted.wav").start();
    }
    else if (jRadioButton10.isSelected()){

            new Player("C:/Users/joel.ramsey/Desktop/Audio for Quotes Program/incharge_converted.wav").start();
    }
    else if (jRadioButton11.isSelected()){
            new Player("C:/Users/joel.ramsey/Desktop/Audio for Quotes Program/break_converted.wav").start();
    }

好的,我接受了建议并尝试实现.getResource方法,但它仍未在包目录“/ my / sounds”中找到文件
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (jRadioButton1.isSelected()){
        URL resource = getClass().getResource("/my/sounds/fear_converted.wav");    
        new Player("/my/sounds/fear_converted.wav").start();
    }

对于那些询问的人,下面是Player类。同样,它在客户端上的文件的绝对路径下也可以正常工作。我没有做到,但是如果我调用.start()方法,它就可以工作。
    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();
        } 

    } 
} 

最佳答案

我想你需要这样的东西
URL resource = Example.class.getResource("/res/1.jpg");
我的项目中/res/1.jpg是文件所在的位置。然后,您可以根据自己的需要获取或Filepath
你可以读this question

编辑:下一个代码对您的Player类来说效果很好,只需使用resource.getFile()而不是resource.toString();

URL resource = QuotesButtonUI.class.getResource("/my/sounds/fear_converted.wav");
String file = resource.getFile();
Player p = new Player(file);
p.start();

关于java - 在GUI应用程序中使用getResourceAsStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19956437/

相关文章:

java - 如何使用多个输入对话框(Java 新手)

java - JDesktopPane 的背景设置

android - 添加 fragment 和简单的UI元素会大大减慢音频处理算法的速度

java - 如何从 dao 函数返回整数?

java - 如何在不使用 xpath 或 CSS 路径的情况下从 Amazon 页面定位元素?

Java 找不到我的文件,即使我很确定我的目录是正确的

java - 使 JComponent 不透明

java - 线程未更新 GlassPane 上的进度栏

javascript - 如何找到 HTMLAudioElement 音频结束播放

audio - 如何实现动态创建mp3?