在GUI应用程序中使用getResourceAsStream

时间:2013-11-13 14:33:42

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

    } 
} 

2 个答案:

答案 0 :(得分:0)

我认为你需要这样的事情

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

答案 1 :(得分:0)

更改您的Player类,将URL作为参数添加到其构造函数中,而不是String。 (无论如何,A String可能是任何东西,所以这不是一个好主意。)