声音没有播放

时间:2017-04-19 07:09:52

标签: java audio nullpointerexception

当我尝试播放声音时,我收到一个错误:

java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)
    at com.artem.megatale.Sound.<init>(Sound.java:13)
    at com.artem.megatale.Sound.<clinit>(Sound.java:7)
    at com.artem.megatale.Level.building(Level.java:340)
    at com.artem.megatale.Level.tick(Level.java:371)
    at com.artem.megatale.Component.tick(Component.java:111)
    at com.artem.megatale.Component.run(Component.java:212)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
    at com.artem.megatale.Sound$1.run(Sound.java:23)
Exception in thread "Thread-5" java.lang.NullPointerException
    at com.artem.megatale.Sound$1.run(Sound.java:23)

我的声音课程:

public class Sound {
    public static final Sound blockBreak = new Sound("/sounds/break_stone.wav");

    private AudioClip clip;

    public Sound(String name) {
        try {
            clip = Applet.newAudioClip(Sound.class.getResource(name));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void play() {
        try {
            new Thread() {
                public void run() {
                    clip.play();
                }
            }.start();
            Thread.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我使用Sound.blockBreak.play();播放声音 声音存储在res / sound / 文件中,并存储在jar文件中。 我尝试将blockBreak = new Sound("/sounds/break_stone.wav");更改为blockBreak = new Sound("res/sounds/break_stone.wav");但我仍然收到错误。

有人要求我的包裹截图:

So here is the screenshot

有人还说我应该摆脱尝试并抓住这样做,并得到另外一个错误:

Exception in thread "Thread-3" java.lang.ExceptionInInitializerError
    at com.artem.megatale.Level.building(Level.java:340)
    at com.artem.megatale.Level.tick(Level.java:371)
    at com.artem.megatale.Component.tick(Component.java:111)
    at com.artem.megatale.Component.run(Component.java:212)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)
    at com.artem.megatale.Sound.<init>(Sound.java:13)
    at com.artem.megatale.Sound.<clinit>(Sound.java:7)
    ... 5 more

com.artem.megatale.Level.building(Level.java:340)

Sound.blockBreak.play();

0 个答案:

没有答案