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 |1 Answer
Reset to default 0Java 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
版权声明:本文标题:game development - Java Sound Exception - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736605273a1945311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
clip
variable perEntity
but you may be using it for multiple clips in theSoundPlayer
inner class which runs as a separate thread. Soclip
is probably being overwritten by the different threads leading to the null pointer exceptions. – greg-449 Commented yesterday