在java游戏中实现声音

在java游戏中实现声音,java,audio,Java,Audio,我跟随Buckys java游戏开发,使用slick进行游戏的基本设置,从那时起,我就自己动手了。目前,我只是建立菜单和它几乎完成,除了我想添加声音到它。我已经在这上面呆了一个星期了,所以我并不是没有对此做任何研究,我只是无法让它工作。现在我已经找到了一些代码来做这件事,这似乎是有意义的,我如何实现这一点,或者如果你有更好的想法,如何添加一些声音,谢谢 import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.

我跟随Buckys java游戏开发,使用slick进行游戏的基本设置,从那时起,我就自己动手了。目前,我只是建立菜单和它几乎完成,除了我想添加声音到它。我已经在这上面呆了一个星期了,所以我并不是没有对此做任何研究,我只是无法让它工作。现在我已经找到了一些代码来做这件事,这似乎是有意义的,我如何实现这一点,或者如果你有更好的想法,如何添加一些声音,谢谢

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);
        }
    }
}
最后一点,我尝试过LWJGL库之类的东西,但我无法理解它们。我也意识到我的要求有多高,因为我基本上是在要求有人把它编码到我的游戏中,我希望我不必这么做,但我不知道如何解决我的问题,而不张贴在这里


另外,这并不是说我自己没有尝试过这样做,所以请不要难过,说我没有做任何努力。

当您尝试从空URL创建AudioInputStream时,会发生此错误。确保url不为空,确保引用了正确的文件。

是否存在任何错误异常或声音无法播放?如果你对游戏开发很认真,那么你应该寻找合适的框架/库来处理低级内容(例如播放声音)。一旦你找到了一个适合你需要的框架,你就可以专注于实际创建你的游戏,而不是重新发明轮子。这实际上取决于你想做什么以及你瞄准的平台。我使用Android游戏(尽管它是跨平台的)。以libgdx为例,框架并不要求我这样做。我不是想成为一个“痛苦者”,我当然不会指责你“不努力”,但我强烈建议你从一些介绍Java的材料开始,比如Deitel的Java How to Program,这样,您就可以更好地理解正在查看的代码,以及面向对象编程的基本知识。为了真正回答您的问题,您创建了一个播放声音片段的方法,但在菜单初始化或呈现期间,您实际上不会在任何地方调用该方法。你需要调用它,它才能真正执行。除了@LJ2的好建议外,当你在创建最简单的用例来测试你的代码时,这也是一个很好的实践。在这种情况下,您可以创建一个只在运行时播放声音的简单程序,这样您就可以进行测试,确保它在简单情况下工作,然后再尝试找出更大情况下的错误。(但是,LJ2已经发现了您的问题,您从未调用
声音
方法。)感谢您的回答,我已经编辑了代码以实现您给我的内容,并将其放在原始问题中。我担心不会产生声音,运行时不会产生错误,但IDE在错误检查时会发现一些问题,我会在我的代码注释中引用这些错误。问题是您的类需要实现Runnable。。。公共类Sound实现Runnablet上述代码是一个独立于类的类。只需在main中创建这个类的一个实例并调用“playBackGround”方法就可以了。您需要创建一个新的java类文件并将上面的代码复制到其中。我理解了,谢谢:)我仍然有一些问题,我将编辑我的问题
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;
    }
}
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;
    }
}