admin管理员组

文章数量:1123890

So after i learned how to create a working runnable .jar file and after some tests, I started changing my program so i can create a .jar file but the main problem is that after some changed the program can't play audio files. I moved the audio files into a sources directory like this:

ROOT
|
|--src
|   | -- com.game.main... etc
|
|--userdata
|
|--resources
|   |-- res
         | -- sound
               |--sound.wav

This logic works and i can create a .jar file When i run it on eclipse it throws exceptions:

java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)

And exceptions:

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:484)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1238)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:115)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1038)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1131)
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:83)
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:484)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1238)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:115)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1038)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1131)
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:83)

This is the code:

public abstract class Entity {
    public int x, y;
    public int width, height;
    public Rectangle rect;
    
    protected int hp;
    protected int strength;
    protected int kills;
    
    public static final int GRAPHIC_BORDER = 6;
    protected boolean drawMore;
    protected Image image;
    protected boolean damaged;
    
    protected Context context;
    protected Color entityColor;
    protected GamePanel gp;
    protected KeyHandler keyH;
    protected Clip clip;
    
    public Entity(Context x, Color ec) {
        entityColor = ec;
        context = x;
        if (x!=null) {
            keyH = x.keyH;
            gp = x.gp;
        }
        damaged = false;
        hp = 100;
        kills = 0;
        
    }
    
    public synchronized void playSound (String path) {
        try {
            URL is = getClass().getResource(path);
            //BufferedInputStream bis = new BufferedInputStream(is);
            SoundPlayer sp = new SoundPlayer(is);
            sp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    class SoundPlayer extends Thread {
        private URL soundFile;
        public SoundPlayer (URL is) {
            this.soundFile = is;
        }
        @Override
        public void run () {
            try {
                if (clip!=null) {
                    //clip.stop();
                    clip.close();
                }
                
                clip = null;
                
                AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
                clip = AudioSystem.getClip();
                clip.open(ais);
                
                clip.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    }

Can someone help me solve this ?????

So after i learned how to create a working runnable .jar file and after some tests, I started changing my program so i can create a .jar file but the main problem is that after some changed the program can't play audio files. I moved the audio files into a sources directory like this:

ROOT
|
|--src
|   | -- com.game.main... etc
|
|--userdata
|
|--resources
|   |-- res
         | -- sound
               |--sound.wav

This logic works and i can create a .jar file When i run it on eclipse it throws exceptions:

java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.start()" because "this.this$0.clip" is null
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:92)

And exceptions:

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:484)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1238)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:115)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1038)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1131)
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:83)
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:484)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1238)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:115)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1038)
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1131)
    at aGame.v2/com.game.lib.entity.Entity$SoundPlayer.run(Entity.java:83)

This is the code:

public abstract class Entity {
    public int x, y;
    public int width, height;
    public Rectangle rect;
    
    protected int hp;
    protected int strength;
    protected int kills;
    
    public static final int GRAPHIC_BORDER = 6;
    protected boolean drawMore;
    protected Image image;
    protected boolean damaged;
    
    protected Context context;
    protected Color entityColor;
    protected GamePanel gp;
    protected KeyHandler keyH;
    protected Clip clip;
    
    public Entity(Context x, Color ec) {
        entityColor = ec;
        context = x;
        if (x!=null) {
            keyH = x.keyH;
            gp = x.gp;
        }
        damaged = false;
        hp = 100;
        kills = 0;
        
    }
    
    public synchronized void playSound (String path) {
        try {
            URL is = getClass().getResource(path);
            //BufferedInputStream bis = new BufferedInputStream(is);
            SoundPlayer sp = new SoundPlayer(is);
            sp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    class SoundPlayer extends Thread {
        private URL soundFile;
        public SoundPlayer (URL is) {
            this.soundFile = is;
        }
        @Override
        public void run () {
            try {
                if (clip!=null) {
                    //clip.stop();
                    clip.close();
                }
                
                clip = null;
                
                AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
                clip = AudioSystem.getClip();
                clip.open(ais);
                
                clip.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    }

Can someone help me solve this ?????

Share Improve this question edited yesterday g00se 4,2862 gold badges6 silver badges14 bronze badges asked yesterday iasonasiasonas 94 bronze badges New contributor iasonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • 2 This question is similar to: Jar File throws FileNotFoundException but eclipse does not. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. If that didn't solve your problem, you should have continued to ask there – g00se Commented yesterday
  • You only have one clip variable per Entity but you may be using it for multiple clips in the SoundPlayer inner class which runs as a separate thread. So clip is probably being overwritten by the different threads leading to the null pointer exceptions. – greg-449 Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

Java doesn't support 48000 AFAIK. I think the highest value supported is 44100. You can use a tool such as Audacity to convert the sound cue. Otherwise the code looks like it should run.

本文标签: game developmentJava Sound ExceptionStack Overflow