Java 带声音类的NullPointerException
我目前正在构建一个游戏引擎,引擎内部有一个类,名为Sound.java。本课程的目的是将声音效果加载到引擎中并播放它们。 问题是在Sound.java的构造函数中,它抛出了一个名为NullPointerException的异常;但是,这没有任何意义,因为我引用的是所述声音文件的正确文件路径 看看下面的代码+堆栈跟踪,请帮我解决这个问题 Sound.java:Java 带声音类的NullPointerException,java,audio,Java,Audio,我目前正在构建一个游戏引擎,引擎内部有一个类,名为Sound.java。本课程的目的是将声音效果加载到引擎中并播放它们。 问题是在Sound.java的构造函数中,它抛出了一个名为NullPointerException的异常;但是,这没有任何意义,因为我引用的是所述声音文件的正确文件路径 看看下面的代码+堆栈跟踪,请帮我解决这个问题 Sound.java: public class Sound { private Clip clip; public Sound(String filePat
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");
}
}
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");
}
}
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)
CESound.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");
}
}
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");
}
}
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)
堆栈跟踪:
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");
}
}
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");
}
}
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)
java.lang.NullPointerException
在com.sun.media.sound.softmidiaAudioFileReader.getAudioInputStream(softmidiaAudioFileReader.java:130)上
位于javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113)
位于CoreEngine.resourcebrary.Other.Sound.(Sound.java:16)
位于CoreEngine.resourcebrary.CESound.addSound(CESound.java:21)
位于ResourceLibrary.ResourceLoader.loadSounds(ResourceLoader.java:48)
在GameStates.MenuState.init(MenuState.java:25)
在gamestate.MenuState.(MenuState.java:21)
在GameStateManager.loadState(GameStateManager.java:27)
在GameStateManager.GameStateManager.(GameStateManager.java:22)
位于GameEngine.Core.init(Core.java:29)
在CoreEngine.GameEngine.CoreEngine.(CoreEngine.java:55)
在GameEngine.Core.(Core.java:24)
位于GameEngine.Core.main(Core.java:62)
谢谢
Nick
< P>(1)考虑使用TyySoad(GITHUB)。免费,易于使用,相当小,基本的图书馆 (2) 我建议使用这种方式(从javax.sound.sampled)来获取声音数据: 或者使用表单AudioSystem.getAudioInputStream(File)
为此,首先定义URL或文件。增加的好处是,在创建AudioInputStream之前,调试/验证您是否确实与该文件联系会更容易。看起来像
(ReferenceLibrary.SoundLocation+fileName)
有一些问题,这意味着AudioSystem.getAudioInputStream未正确创建实例。您确定此行没有给出任何异常AudioFormat baseFormat=ais.getFormat()代码>错误消息引用的代码行是什么?为什么要引用com.sun.media.sound.softmidiaAudioFileReader.getAudioInputStream?您需要的一切都应该来自java.sound.sampled库。我认为sun.media.sound的东西已经被弃用了。