Java OggStreamer线程类(在我的游戏中播放音乐)仅在1/3的时间内有效

Java OggStreamer线程类(在我的游戏中播放音乐)仅在1/3的时间内有效,java,stream,ogg,Java,Stream,Ogg,首先,我爱你们!这是寻找奇怪和困难编程问题答案的最佳网站。这是我在这个网站上找不到解决方案的第一个问题,谢谢 因此,我有一个runnable Game.class和一个runnable OggStreamer.class,允许音乐在它自己的单独线程中运行,我将其作为参数发送给Game类使用。当我从IDE运行游戏时,OggStreamer总是有效的,但是当我将它导出到一个.jar文件时,它只在我启动它的1/3次中有效。这并不是说第一段音乐没有开始,然后下一段音乐开始播放……它根本不起作用,直到我开

首先,我爱你们!这是寻找奇怪和困难编程问题答案的最佳网站。这是我在这个网站上找不到解决方案的第一个问题,谢谢

因此,我有一个runnable Game.class和一个runnable OggStreamer.class,允许音乐在它自己的单独线程中运行,我将其作为参数发送给Game类使用。当我从IDE运行游戏时,OggStreamer总是有效的,但是当我将它导出到一个.jar文件时,它只在我启动它的1/3次中有效。这并不是说第一段音乐没有开始,然后下一段音乐开始播放……它根本不起作用,直到我开始游戏几次

你们这些好人有没有遇到过类似的问题?如果它根本不起作用,我可以理解,这可能表明jar文件中对音乐的文件引用有问题……但它确实起作用,只是不一致

注意:这是我第一次尝试游戏编程,我知道它不太好看,而且我是一个完全的新手:)关于总体设计,我会改变很多东西,但我把它作为一个项目来使用,以帮助我理解当我坐下来为我的下一个游戏设计一个真正的框架时会遇到的问题

开始上课

public static void main(String[] args) throws InterruptedException{
ExecutorService threadExecutor = Executors.newCachedThreadPool();
Game game = new Game(musicStreamer);
game.init(); // loads stuff
threadExecutor.execute( musicStreamer ); // start task1
threadExecutor.execute( game ); // start task2
threadExecutor.shutdown();
}
OggStreamer.class

public class OggStreamer implements Runnable{

private URL url;
private AudioInputStream stream;
private AudioInputStream decodedStream;
private AudioFormat format;
private AudioFormat decodedFormat;
private boolean stop, running;
String filename = "";
SourceDataLine line = null;

public OggStreamer() {
    this.stop = true;
    this.running = true;
    this.url = null;
}

public void run() {
    while(running){
            while (!this.stop) {
                System.out.println("Playing Loop");
                try {
                    // Get AudioInputStream from given file.
                    this.stream = AudioSystem.getAudioInputStream(this.url);
                    this.decodedStream = null;
                    if (this.stream != null) {
                        this.format = this.stream.getFormat();
                        this.decodedFormat = new AudioFormat(
                                AudioFormat.Encoding.PCM_SIGNED,
                                this.format.getSampleRate(), 16,
                                this.format.getChannels(),
                                this.format.getChannels() * 2,
                                this.format.getSampleRate(), false);
                        // Get AudioInputStream that will be decoded by underlying
                        // VorbisSPI
                        this.decodedStream = AudioSystem.getAudioInputStream(
                                this.decodedFormat, this.stream);
                    }else{
                        JOptionPane.showMessageDialog(null, "Stream = null!");
                    }
                } catch (Exception e) {
                    // Do nothing
                    System.out.println("Could not get or decode audiostream");
                }
                line = null;
                try {
                    line = this.getSourceDataLine(this.decodedFormat);
                    FloatControl volume = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
                    volume.setValue(1);
                } catch (LineUnavailableException lue) {
                    // Do nothing
                    JOptionPane.showMessageDialog(null, "Line is unavailable!");
                }
                if (line != null) {
                    try {
                        byte[] data = new byte[4096];
                        // Start
                        line.start();
                        int nBytesRead = 0;

                        while (nBytesRead != -1) {
                            nBytesRead = this.decodedStream.read(data, 0,
                                    data.length);
                            if (nBytesRead != -1) {
                                line.write(data, 0, nBytesRead);
                            }
                            if (this.stop) {
                                break;
                            }
                        }

                        // Stop
                        line.drain();
                        line.stop();
                        line.close();
                    } catch (IOException io) {
                        // Do nothing
                        JOptionPane.showMessageDialog(null, "Line cannot start!");
                    }
                }
            }
    }
}

private SourceDataLine getSourceDataLine(AudioFormat audioFormat)
        throws LineUnavailableException {
    SourceDataLine res = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    res = (SourceDataLine) AudioSystem.getLine(info);
    res.open(audioFormat);
    return res;
}


public void startLoop(String filenameString) {
    this.filename = filenameString;
    System.out.println("Starting loop with: "+filenameString);
    this.url = this.getClass().getResource(filenameString);
    this.stop = false;
}

public void stopLoop() {
    System.out.println("Stopping loop");
    try {
        if(this.decodedStream!=null)this.decodedStream.close();
        if(this.stream!=null)this.stream.close();
    } catch (IOException e) {
    }
    this.stop = true;
    this.url = null;
}

public boolean isStop() {
    return stop;
}

public void setStop(boolean stop) {
    this.stop = stop;
}

public URL getUrl() {
    return url;
}

public void setUrl(String string) {
    this.filename = string;
    this.url = this.getClass().getResource(string);
}

public String getFilename() {
    return filename;
}
}
Game.class使用OggStreamer类:

if(musicStreamer!=null && !musicStreamer.getFilename().equals("/snm/sound/oggs/Prelude.ogg")){
            if(!musicStreamer.isStop())musicStreamer.stopLoop();
            musicStreamer.startLoop("/snm/sound/oggs/Prelude.ogg");
}