在Java游戏中实现声音

2ekbmq32  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(197)

我已经跟随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);
        }
    }
}

}
edit:上面的代码现在在'init()'方法中创建了一个对象,该对象调用'Sound'类及其方法'playBackGround()'。Sound类的代码在下面JavaNewb的答案中给出。
edit:此代码产生的错误如下:

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库,我不能使他们的意义。我也意识到多少我要求,因为我基本上是要求有人把这个编码到我的游戏,我希望我没有这样做,但我不知道如何解决我的问题,而不是张贴在这里。
附言:我自己也不是没有试过,所以不要痛苦地说我没有努力

j1dl9f46

j1dl9f461#

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;
    }
}
wn9m85ua

wn9m85ua2#

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

相关问题