0

I have a character quotes application in which the GUI class calls a player class that handles the .wav files. A user uses a radio button to select a quote and hits the play button to initiate. The application works fine when the path to the .wav is defined as an absolute path on the system. I want to incorporate the .wav files into a package my.sounds as "/my/sounds/anyfilename.wav" I know that I need to use the getResourceAsStream() method but I don't know how to incorporate it into the GUI class when calling the Player class. Again, the Player class works fine with absolute paths. The error is a file not found error.

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

Okay, I took advice and tried to implement a .getResource method but its still not finding the file in the package directory "/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();
    }

For those who were asking, the Player class is below. Again, it works fine with an absolute path to the file on the client. I didn't make it but it works if I call the .start() method.

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

    } 
} 
Joel
  • 87
  • 2
  • 12
  • It's very hard to answer this question definitively without knowing anything about this `Player` class. Did you write it? If not, is it publicly available? – Robin Green Nov 13 '13 at 20:27

2 Answers2

0

I think you need somthing like this

URL resource = Example.class.getResource("/res/1.jpg");

where /res/1.jpg is file in my project. Then you can get or File, or path to it, for your purposes

you can read this question

EDIT: Next code works fine for your Player class, just use resource.getFile() instead of resource.toString();

URL resource = QuotesButtonUI.class.getResource("/my/sounds/fear_converted.wav");
String file = resource.getFile();
Player p = new Player(file);
p.start();
Community
  • 1
  • 1
alex2410
  • 10,424
  • 3
  • 22
  • 40
  • So how do you take the return on the resource call and plug it into the Player object call to start? It looks like URL resource = QuotesButtonUI.class.getResource("/my/sounds/fear_converted.wav");new Player (resource.toString()).start(); This is passing an absolute file path to the Player start method, but its still indicating that it can't find it. – Joel Nov 14 '13 at 06:34
  • Yes, I discovered that a few minutes ago using this: if (jRadioButton1.isSelected()){ URL file = QuotesButtonUI.class.getResource("/my/sounds/fear_converted.wav"); new Player (file.getPath()).start(); } – Joel Nov 14 '13 at 08:02
0

Change your Player class to take a URL as an argument to its constructor, instead of a String. (A String could be anything, anyway, so it's not a good idea.)

Robin Green
  • 29,408
  • 13
  • 94
  • 178