Вот упрощенная версия моего кода, в которой я пытаюсь успешно прочитать изображение Baker.png и прочитать аудиофайл QuestOfLegends.mp3 неудачно.
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);
}
}
}
Я пробовал использовать:
Код: Выделить всё
File file = new File(Main.class.getResource("/audio/QuestOfLegends.mp3"));
Подробнее здесь: https://stackoverflow.com/questions/786 ... ot-for-png