无法在Java中播放声音

时间:2013-11-25 19:17:28

标签: java resources directory audio

我正在尝试为我的Java游戏添加声音效果而我没有这样做。我制作了Sound类,我有.wav文件,但由于某种原因,我听不到声音,我收到了关于我的代码,未知来源或其他内容的几个错误,我不确定。我将为你们提供Sound类,我的课堂调用和错误。

我的文件夹如下所示: “... \ Java Workspace \ SpaceShooter \ src” - 对于我的Java .class文件。 “... \ Java Workspace \ SpaceShooter \ src \ sounds” - 对于我的java声音。 我正在使用的IDE是Eclipse。

这是Sound类

import java.applet.Applet;
import java.applet.AudioClip;

public class Sound {
    public static final Sound laser = new Sound("sounds/laser.wav");
    private AudioClip clip;

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

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

这就是我调用Sound类的方法。

...
if (code == KeyEvent.VK_UP) 
    if (!player1weapon.isFalling){
         player1weapon.isFalling = true;
         Sound.laser.play();
    }

错误

java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)
    at Sound.<init>(Sound.java:10)
    at Sound.<clinit>(Sound.java:5)
    at SpaceInvadersPanel$4.keyPressed(SpaceInvadersPanel.java:71)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "Thread-2" java.lang.NullPointerException
    at Sound$1.run(Sound.java:20)

1 个答案:

答案 0 :(得分:1)

错误说明了一切

java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)

此处发生错误:

clip = Applet.newAudioClip(Sound.class.getResource(filename)); 

所以要么filename是nullpointer,要么Sound.class.getResource(filename)的返回值是nullpointer。

尝试通过打印来检查这些值并查看它们是什么。

Sound sound = Sound.class.getResource(filename);

System.out.println(sound);
System.out.println(filename);

if (filename != null && sound != null) clip = Applet.newAudioClip(sound);