我有一个字符引用应用程序,其中 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();
}
}
}