Simple GUI application for playing sound clips after user selects one with a radio button and pushes the play button. After clean and build, execution from the JAR file results in no sound being played when a clip is selected and the play button is pushed.

Conditions: NetBeans IDE, sounds play successfully in the IDE pathed to the package, path to .wav files in JAR is correct, files are in the executable JAR in the correct directory, uses 2 classes: one for the GUI and a .wav handler class (both work successfully in the IDE. More details in screen shots. I am thinking that there should be a getResource() method call in the Player calss but I don't know how to write it.

The code snippet used to call the resource from within the GUI class. The base is new Player("whateverthefilepathis").start(). (this works fine in the IDE, so no issue there):

This is the Player class used to process the .wav. Within the GUI class, I am using the new Player().start() call. I am thinking that there should be a getResource() call in the Player class but I don't know for sure.

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

}

}

}

网友答案:

You can't access anything inside a jar file using the java.io.File API, simply because items inside a jar are not files.

You need indeed, as you suspected, to use a getResource() method (or getResourceAsStream()): http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

That may look counterintuitive at first, but getResource() works with files as well as jars (or even remote located resources in case of a webapp, as in the linked tutorial), the ClassLoader will deal with the dirty details how the resource is physically accessed. In short: never use the File API for resources - resources should only be accessed using the resource API.