所以我一直在尝试在我的游戏中添加声音,但是在读取资源流时初始化我的 AudioPlayer 类时出现错误。我在另一个游戏项目上对此进行了测试,它运行良好,没有错误,所以这很奇怪。这是我的 AudioPlayer 代码:
public class AudioPlayer
{
private Clip clip;
public AudioPlayer(String s)
{
try{
AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(s));
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//methods here
}
这是错误日志:
Exception in thread "main" java.util.ServiceConfigurationError: javax.sound.sampled.spi.AudioFileReader: Error reading configuration file
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.parse(ServiceLoader.java:309)
at java.util.ServiceLoader.access$200(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:357)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at com.sun.media.sound.JSSecurityManager$3.run(JSSecurityManager.java:180)
at com.sun.media.sound.JSSecurityManager$3.run(JSSecurityManager.java:178)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.media.sound.JSSecurityManager.getProviders(JSSecurityManager.java:184)
at com.sun.media.sound.JDK13Services.getProviders(JDK13Services.java:109)
at javax.sound.sampled.AudioSystem.getProviders(AudioSystem.java:1631)
at javax.sound.sampled.AudioSystem.getAudioFileReaders(AudioSystem.java:1406)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1107)
at main.audio.AudioPlayer.<init>(AudioPlayer.java:18)
at main.states.Game.<init>(Game.java:65)
at main.states.Game.main(Game.java:291)
Caused by: java.io.FileNotFoundException: C:\Users\Matthew\Desktop\lots of fun (The system cannot find the file specified)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:219)
at java.util.zip.ZipFile.<init>(ZipFile.java:149)
at java.util.jar.JarFile.<init>(JarFile.java:166)
at java.util.jar.JarFile.<init>(JarFile.java:103)
at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:94)
at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:150)
at java.net.URL.openStream(URL.java:1045)
at java.util.ServiceLoader.parse(ServiceLoader.java:304)