I’m developing a game in Swing, where I’d like to use both audio and image assets. My issue is that, while opening the .png images works fine, opening my .mp3 file always throws a FileNotFoundException
, and file.exists()
returns false
.
Nothing seems to work. I’ve researched the issue, looked at a similar question, played around with adding and removing the ‘/’ from the filePath, changing the mp3 directory, created a new IntelliJ project, restarted my PC, and tried opening a .wav file instead, with no success.
Here’s a simplified version of my code, where I try to read an image Baker.png
successfully, and read an audio file QuestOfLegends.mp3
unsuccessfully.
Main.java
import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
public class Main {
public static void main(String[] args) {
String imagePath = "/images/Baker.png";
String audioPath = "/audio/QuestOfLegends.mp3";
System.out.println("The absolute path of this class is: " + new File(".").getAbsolutePath());
System.out.println("The audio package is a directory?: " + new File("/audio").isDirectory());
System.out.println("The images package is a directory?: " + new File("/images").isDirectory());
try {
BufferedImage image = ImageIO.read(Objects.requireNonNull(Main.class.getResource(imagePath)));
System.out.println("Image read successful.");
} catch (IOException e) {
System.out.println("Image read failed.");
}
try {
File file = new File(audioPath);
if (!file.exists()) {
throw new Exception("File does not exit at "+ audioPath);
} else {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
} catch (Exception e) {
System.out.println("Read failed");
throw new RuntimeException(e);
}
}
}
Here’s my directory hierarchy.
soybean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.