我正在使用Java进行单人纸牌游戏,我需要在桌面洗牌,卡片翻转等时实现声音。我使用以下网站作为参考尝试让它工作,但我得到了Null指针异常或错误处理的URL异常(取决于我调整的内容)
here
另外,我使用netbeans 6.7.1作为我的IDE
我将尝试分解代码并解释:
package cardgame; import java.applet.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JApplet; import javax.swing.*; import java.io.*; import java.net.*; /** * * @author jacob */ public class Sound extends JApplet { private AudioClip song; // Sound player private String URL = null; private URL songPath; // Sound path /* *sound_1 = shuffling cards *sound_2 = to discard *sound_3 = from discard *sound_4 = cardflip 1 *sound_5 = cardflip 2 */ Sound(String filename) { try { songPath = new URL(getCodeBase(),filename); // Get the Sound URL } catch (MalformedURLException ex) { Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex); } song = getAudioClip(songPath); // Load the Sound } Sound(int i) { URL = "./sounds/sound_" + i + ".wav"; System.out.println(URL); try { songPath = new URL(URL); // Get the Sound URL song = getAudioClip(songPath); } catch (MalformedURLException ex) { Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex); } } public void playSound() { song.loop(); // Play } public void stopSound() { song.stop(); // Stop } public void playSoundOnce() { song.play(); // Play only once } }
我尝试实现这两种不同的构造方法。第一个创建文件路径,并将其传入。第二个在构造函数中构建文件路径,给出声音#(我列出了哪些数字对应于什么声音以供参考)。我收到了以下错误:
./sounds/sound_1.wav Nov 16, 2009 4:14:13 PM cardgame.Sound ./sounds/sound_2.wav SEVERE: null java.net.MalformedURLException: no protocol: ./sounds/sound_1.wav ./sounds/sound_3.wav at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(URL.java:429) ./sounds/sound_4.wav ./sounds/sound_5.wav at cardgame.Sound.(Sound.java:46) at cardgame.Game.loadSounds(Game.java:712) at cardgame.Game.(Game.java:62) at cardgame.Main.main(Main.java:25) Nov 16, 2009 4:14:13 PM cardgame.Sound SEVERE: null java.net.MalformedURLException: no protocol: ./sounds/sound_2.wav at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(URL.java:429) at cardgame.Sound.(Sound.java:46) at cardgame.Game.loadSounds(Game.java:712) at cardgame.Game.(Game.java:62) at cardgame.Main.main(Main.java:25) Nov 16, 2009 4:14:13 PM cardgame.Sound SEVERE: null java.net.MalformedURLException: no protocol: ./sounds/sound_3.wav at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(URL.java:429) at cardgame.Sound.(Sound.java:46) at cardgame.Game.loadSounds(Game.java:712) at cardgame.Game.(Game.java:62) at cardgame.Main.main(Main.java:25) Nov 16, 2009 4:14:13 PM cardgame.Sound SEVERE: null java.net.MalformedURLException: no protocol: ./sounds/sound_4.wav at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(URL.java:429) at cardgame.Sound.(Sound.java:46) at cardgame.Game.loadSounds(Game.java:712) at cardgame.Game.(Game.java:62) at cardgame.Main.main(Main.java:25) Nov 16, 2009 4:14:13 PM cardgame.Sound SEVERE: null java.net.MalformedURLException: no protocol: ./sounds/sound_5.wav at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(URL.java:429) at cardgame.Sound.(Sound.java:46) at cardgame.Game.loadSounds(Game.java:712) at cardgame.Game.(Game.java:62) at cardgame.Main.main(Main.java:25)
感谢那些阅读并感谢那些帮助的人。我知道它有点长,但我宁愿把它全部拿出来,而不是因为缺乏初始信息而有50个问题回来或让人们不回答。也只允许我现在发布一个链接,所以链接在下面给出
dreamincode.net/forums/showtopic14083.htm
stackoverflow.com/questions/512436/java-playing-wav-sounds
deitel.com/articles/java_tutorials/20060422/LoadingPlayingAudioClips/index.html
答案 0 :(得分:0)
根据堆栈跟踪,您显然没有在applet查看器或浏览器插件中运行applet,而是通过Main和Game类实例化自己。在这种情况下,getCodeBase将返回null(如果applet实际作为applet运行,则返回Java代码的基本URL)。因此,当您尝试基于未定义起点(getCodeBase()的结果)的相对路径创建URL时,URL类会正确地抱怨。