我正在尝试播放.wav文件,但是由于某些原因,我总是遇到找不到错误的错误。下面是我的代码:
package Program;
public class YuanVideoGameStore {
//VARIABLES:
static final String BACKGRUONDMUSIC = "/YuanVideoGameStore/src/reasources/b2.wav";
public static void main(String[] args) {
PlayMusic play = new PlayMusic();
play.BackGroundSound(BACKGRUONDMUSIC);
}
}
package Program;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;
public class PlayMusic {
static Clip clip;
void BackGroundSound(String musicLocation) {
try {
File musicPath = new File(musicLocation);
if(musicPath.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
JOptionPane.showMessageDialog(null, "Please don't press the button");
}else {System.out.print("Cannot find file!");}
}catch(Exception ex) {ex.printStackTrace();}
}
}
我必须使用相关路径调用文件,因为此程序将交给我的老师。我已经尝试了很多次,但是音乐文件仍然没有播放。请帮忙。