声音不会在java中播放.. JFrame和Canvas不是Applet

时间:2013-07-13 13:16:31

标签: java canvas embedded-resource javasound audio

我目前正在开发我的第一个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();
    }
}
}

这是stacktrace

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();
        }
    }
}

感谢您的建议现在可行

2 个答案:

答案 0 :(得分: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);

路径String再次取决于您的类文件相对于src目录的位置。

答案 1 :(得分: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

希望这有帮助,