-1

我目前正在开发我的第一个 java 游戏,我正在尝试在飞船被击中时实现声音.. 这是我的代码。我得到一个空指针异常,但我的声音在正确的位置“workspace/project/src/sounds/”

public class GameSounds 
 {
public static synchronized void hit()
{
    try
    {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(GameSounds.class.getResourceAsStream("sounds/8bit_bomb_explosion.wav"));
        clip.open(inputStream);
        clip.start(); 
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

这是堆栈跟踪

java.lang.NullPointerException
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at sound.GameSounds.hit(GameSounds.java:16)
    at main.Main.doLogic(Main.java:135)
    at main.Main.run(Main.java:101)
    at java.lang.Thread.run(Unknown Source)

package sound;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class GameSounds 
{
    public static synchronized void hit()
    {
        try
        {
            String resPath = "/sounds/8bit_bomb_explosion.wav"; // *** this is the key ***
            InputStream audioInStream = GameSounds.class.getResourceAsStream(resPath);
            System.out.println("is audioInStream null?: " + (audioInStream == null)); // test it!
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioInStream);          Clip clip = AudioSystem.getClip();
            clip.open(inputStream);
            clip.start(); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

感谢您的建议,现在可以了

4

2 回答 2

1

很可能,您没有获得具有正确路径的资源。了解资源路径基于类加载器加载类文件的位置,而不是 src 或“user.dir”目录的位置。

也许你想做:

// almost always better to break up a long code line into smaller lines.
String resPath = "/sounds/8bit_bomb_explosion.wav"; // *** this is the key ***
InputStream audioInStream = GameSounds.class.getResourceAsStream(resPath);
System.out.println("is audioInStream null?: " + (audioInStream == null)); // test it!
AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioInStream);

再次,路径字符串将取决于您的类文件相对于您的 src 目录的位置。

于 2013-07-13T13:26:49.117 回答
0

我有同样的问题。但是在搜索了几个小时后,我发现了 Javazoom,它是一个外部 libery,您可以将其导入您的项目并使其更容易播放声音: http ://www.javazoom.net/index.shtml

你可以像这样使用它:

  import javazoom.jl.player.advanced.*;

class SoundJLayer extends PlaybackListener implements Runnable
{
    private String filePath;
    private AdvancedPlayer player;
    private Thread playerThread;    

    public SoundJLayer(String filePath)
    {
        this.filePath = filePath;
    }

    public void play()
    {
        try
        {
            String urlAsString = 
                "file:///" 
                + new java.io.File(".").getCanonicalPath() 
                + "/" 
                + this.filePath;

            this.player = new AdvancedPlayer
            (
                new java.net.URL(urlAsString).openStream(),
                javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice()
            );

            this.player.setPlayBackListener(this);

            this.playerThread = new Thread(this, "AudioPlayerThread");

            this.playerThread.start();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void stop() {
        player.stop();
    }
    // PlaybackListener members

    public void playbackStarted(PlaybackEvent playbackEvent)
    {
    }

    public void playbackFinished(PlaybackEvent playbackEvent)
    {
    }    

    // Runnable members

    public void run()
    {
        try
        {
            this.player.play();
        }
        catch (javazoom.jl.decoder.JavaLayerException ex)
        {
            ex.printStackTrace();
        }

    }
}

之后,您只需创建一个新的 SoundJLayer-Object 并使用 play() 启动 ir

希望这有帮助,

于 2013-07-13T13:29:47.327 回答