gpt4 book ai didi

java - NullPointerException w/Sound 类

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:58 25 4
gpt4 key购买 nike

我目前正在构建一个游戏引擎,引擎内部有一个类,名为 Sound.java。该类的目的是将声音效果加载到引擎中并播放它们。问题是在 Sound.java 的构造函数内,它抛出一个名为 NullPointerException 的异常;但是,这没有任何意义,因为我引用的是所述声音文件的正确文件路径。

看看下面的代码+堆栈跟踪,请帮我解决这个问题!

声音.java:

public class Sound {
private Clip clip;

public Sound(String filePath) {
try {
System.out.println(filePath);
AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(filePath));
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();
CEDebug.showDebugMessage("Sound File Not Found");
}
}

CESound.java:

public class CESound {

private static Map<String, Sound> soundMap = new HashMap<String, Sound>(); //Sound Effects HashMap

/**
* Adds a new sound into the hash map<br>
* The file must be <strong>.wav</strong> format
* @param key the key or ID that will be used to access this sound effect
* @param fileName the name of the sound file
*/
public static void addSound(String key, String fileName) {
try {
soundMap.put(key, new Sound(ReferenceLibrary.SoundLocation + fileName)); //Error: (3/4/15) Sound File Not Found!
} catch (Exception e) {
e.printStackTrace();
CEDebug.showDebugMessage("Sound File Not Found");
}
}

ReferenceLibrary.java:

public class ReferenceLibrary {
public static final String ResourceLocation = "./Resources/";
public static final String SoundLocation = ResourceLocation + "Audio/Sound/";
}

堆栈跟踪:

java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:130)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113)
at CoreEngine.ResourceLibrary.Other.Sound.<init>(Sound.java:16)
at CoreEngine.ResourceLibrary.CESound.addSound(CESound.java:21)
at ResourceLibrary.ResourceLoader.loadSounds(ResourceLoader.java:48)
at GameStates.MenuState.init(MenuState.java:25)
at GameStates.MenuState.<init>(MenuState.java:21)
at GameStates.GameStateManager.loadState(GameStateManager.java:27)
at GameStates.GameStateManager.<init>(GameStateManager.java:22)
at GameEngine.Core.init(Core.java:29)
at CoreEngine.GameEngine.CoreEngine.<init>(CoreEngine.java:55)
at GameEngine.Core.<init>(Core.java:24)
at GameEngine.Core.main(Core.java:62)

谢谢!

-尼克

最佳答案

(1) 考虑使用 TinySound(在 github 上)。免费、易于使用、非常小、基本的库。

(2) 我建议使用这种方式(来自 javax.sound.sampled)来获取声音数据: AudioSystem.getAudioInputStream(URL)

或使用表格

AudioSystem.getAudioInputStream(File)

为此,请首先定义 URL 或文件。额外的好处是,在创建 AudioInputStream 之前,可以更轻松地调试/验证您是否实际接触过该文件。

关于java - NullPointerException w/Sound 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29391057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com