带声音的 NullPointerException Class
NullPointerException w/ Sound Class
我目前正在构建一个游戏引擎,引擎内部有一个名为 Sound.java 的 class。 class 的目的是将音效加载到引擎中并播放。
问题是在 Sound.java 的构造函数中,它抛出了一个名为 NullPointerException 的异常;但是,这没有任何意义,因为我指的是上述声音文件的正确文件路径。
看看下面的代码+堆栈跟踪,请帮我解决这个问题!
Sound.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 或文件。增加的好处是,debug/verify 在创建 AudioInputStream 之前是否实际接触过文件会更容易。
我目前正在构建一个游戏引擎,引擎内部有一个名为 Sound.java 的 class。 class 的目的是将音效加载到引擎中并播放。 问题是在 Sound.java 的构造函数中,它抛出了一个名为 NullPointerException 的异常;但是,这没有任何意义,因为我指的是上述声音文件的正确文件路径。
看看下面的代码+堆栈跟踪,请帮我解决这个问题!
Sound.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 或文件。增加的好处是,debug/verify 在创建 AudioInputStream 之前是否实际接触过文件会更容易。