Reputation: 35
I am trying to play the .wav file but for some reason, I am always getting the error of can't find the error. Below is my code:
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();}
}
}
I had to call on the file using a related path because this program is going to be handed to my teacher. And I have already tried many times but the music file is still not being played. Please help.
Upvotes: 0
Views: 23