我已经跟随Buckys java游戏开发,为游戏的基本设置添加了光滑的功能,从那以后我就把它掌握在了自己的手中。目前我只是建立菜单,它几乎完成,除了我想添加声音。我已经坚持了一个星期所以它不像我没有做过任何研究,我只是无法使它工作。现在我已经找到了一些代码来做这个似乎有意义,我怎么能实现这个,或者如果你有更好的想法如何添加一些声音,赞赏。
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
static String soundtrack = "res/doxx.wav";
public void sound(String path){
try{
AudioInputStream audio = AudioSystem.getAudioInputStream(Menu.class.getResource(path));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
} catch (Exception e){
System.out.println("check "+path+"\n");
e.printStackTrace();
}
}
编辑:此代码现已从程序中删除,但可以随意发布答案
这是与添加声音相关的代码到目前为止,这里是我的菜单类的全部,它不是很大。说实话,你可能不需要详细看它,它目前都是基于图形和两个旋转的按钮(启动和退出)。
package javagame;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
double pi=3.14159265359;
float beanPosY = 330;
float beanPosX = 70;
double gravity = 0.01;
double angleStart=1.5*pi;
double angleQuit=0.5*pi;
int radius=120;
int centerX=300;
int centerY=160;
float startPosX = (float) (centerX + Math.sin(angleStart)*radius);
float startPosY = (float) (centerY + Math.cos(angleStart)*radius);
float quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
float quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);
double force = 0;
public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
Sound music = new Sound();
music.playBackGround("res/doxx.wav");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
Image background = new Image("res/background640x480.fw.png");
g.drawImage(background, 0, 0);
Image start = new Image("res/Start100x100.fw.png");
Image quit = new Image("res/quit100x100.fw.png");
start.draw(startPosX,startPosY);
quit.draw(quitPosX,quitPosY);
Image grass = new Image("res/grass640x150.fw.png");
g.drawImage(grass,0,340);
Image bean = new Image("res/bean.jpg");
bean.draw(beanPosX, beanPosY);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
int posX = Mouse.getX();
int posY = Mouse.getY();
double constant=0.002*pi;
startPosX = (float) (centerX + Math.sin(angleStart)*radius);
startPosY = (float) (centerY + Math.cos(angleStart)*radius);
quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);
angleStart+=constant;
angleQuit+=constant;
if (angleStart>=2*pi){
angleStart-=2*pi;
}
if (angleQuit>=2*pi){
angleQuit-=2*pi;
}
//button interactions
menuInteraction(posX,posY,sbg);
if (beanPosY>=330){
force=1;
}
beanPosY-=force;
force-=gravity;
}
public int getID(){
return 0;
}
private void menuInteraction(int posX, int posY, StateBasedGame sbg){
//play button
float startXDist=posX-(startPosX+50);
float startYDist=(480-posY)-(startPosY+50);
float startDist=(float) Math.sqrt((startXDist*startXDist)+(startYDist*startYDist));
if(startDist<=50){
if(Mouse.isButtonDown(0)){
sbg.enterState(1);
}
}
//quit button
float quitXDist=posX-(quitPosX+50);
float quitYDist=(480-posY)-(quitPosY+50);
float quitDist=(float) Math.sqrt((quitXDist*quitXDist)+(quitYDist*quitYDist));
if(quitDist<=50){
if(Mouse.isButtonDown(0)){
System.exit(0);
}
}
}
}
编辑:上面的代码现在在'init()'方法中创建一个对象,该方法调用'Sound'类及其方法'playBackGround()'。 Sound类的代码在JavaNewb的答案中给出。
编辑:此代码产生的错误如下:
java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at javagame.Sound.run(Sound.java:45)
at java.lang.Thread.run(Unknown Source)
最后一点,我已经尝试过诸如LWJGL库之类的东西,我无法理解它们。另外我意识到我要问的是多少,因为我基本上要求有人将这个编码到我的游戏中,我希望我不必这样做但我不知道怎么解决我的问题而不在这里发布
PS。它不像我自己没有尝试过这样做,所以不要痛苦,说我没有付出努力
答案 0 :(得分:0)
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
public class Sound implements Runnable
{
private boolean running = false;
private Thread thread;
public Sound()
{
this.start();
}
public void start()
{
if(running)
return;
this.thread = new Thread(this);
this.running = true;
this.thread.start();
}
//
private boolean playSong = false;
private AudioInputStream inputStream;
private String url;
private Clip clip;
@Override
public void run()
{
while(running)
{
if(inputStream == null && playSong)
{
this.playSong = false;
try
{
this.inputStream = AudioSystem.getAudioInputStream(Sound.class.getResource(url));
this.clip.open(inputStream);
this.clip.loop(10);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
public void playBackGround(String string) // call to play .wav file
{
if(this.clip != null)
{
this.clip.stop();
this.clip.close();
}
try
{
this.clip = AudioSystem.getClip();
}
catch(LineUnavailableException e)
{
e.printStackTrace();
}
url = string + ".wav";
this.playSong = true;
this.inputStream = null;
}
public void disposeSound()
{
if(this.clip != null)
{
this.clip.stop();
this.clip.close();
}
this.clip = null;
this.playSong = false;
this.inputStream = null;
}
}
答案 1 :(得分:0)
当您尝试从空URL创建AudioInputStream时,会发生此错误。确保url不为null,请确保引用正确的文件。