Java 在我的Eclipse项目中包含SWT JAR会破坏不符合';不要使用SWT

Java 在我的Eclipse项目中包含SWT JAR会破坏不符合';不要使用SWT,java,eclipse,swt,Java,Eclipse,Swt,我被难住了。我编写了一个非常基本的程序来测试我们在一个单独的Eclipse项目中开发的MP3抽象。MP3类有3个依赖项,我将它们转移到主项目中,我们也复制了这个类。然而,当尝试播放新项目的歌曲时,代码突然停止工作。修复?卸下SWT罐。我不明白为什么它能工作,但删除SWT会使MP3播放,再加上它会再次中断播放。swt.jar中唯一包含的东西是org.eclipse.swtcode,这已通过JD-GUI反编译得到证实。问题是没有任何音频代码从org.eclipse.swt导入任何内容。你知道如何添加

我被难住了。我编写了一个非常基本的程序来测试我们在一个单独的Eclipse项目中开发的MP3抽象。MP3类有3个依赖项,我将它们转移到主项目中,我们也复制了这个类。然而,当尝试播放新项目的歌曲时,代码突然停止工作。修复?卸下SWT罐。我不明白为什么它能工作,但删除SWT会使MP3播放,再加上它会再次中断播放。swt.jar中唯一包含的东西是
org.eclipse.swt
code,这已通过JD-GUI反编译得到证实。问题是没有任何音频代码从org.eclipse.swt导入任何内容。你知道如何添加SWT文件可以完全破坏MP3代码吗

我的测试文件:

MP3 mp3 = new MP3(new File("four-chord-song.mp3"));

mp3.play(60);

Scanner in = new Scanner(System.in);
long lastTime = System.currentTimeMillis();
while (true) {
    in.nextLine();
    System.out.println(System.currentTimeMillis() - lastTime);
    lastTime = System.currentTimeMillis();
}

还有MP3文件(有点凌乱,SWT挂起的那一行被注意到了):


我很困惑是怎么回事,但我重新下载了最新的SWT,它成功了,尽管在启动时我不得不添加
-XstartOnFirstThread
参数,或者我得到了
***警告:由于可可限制,必须在主线程上创建显示。
错误。

(有点乱,注意到了SWT的挂线):“那条线在哪里?没有注明,对不起,我修好了。具体的行是:
line=(SourceDataLine)javax.sound.sampled.AudioSystem.getLine(info)
可以简化为
line=AudioSystem.getSourceDataLine(decodedFormat,null)具有相同的效果如何将SWT.jar添加到Eclipse以及它是什么版本的SWT?您知道它依赖于平台吗?我通过右键单击SWT jar并转到构建路径-->添加到构建路径来添加SWT jar。是的,我使用的是OS X 10.8.2,并且使用的是正确版本的SWT
package com.partyrock.music;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.*;

public class MP3 extends Sound {

private File file;
private boolean isPaused;
private boolean isPlaying;
private AudioFileFormat fileFormat;
private Thread MP3Thread;
private SourceDataLine line;
private AudioInputStream din;
private MP3Player myMP3Player;
private double startTime = 0;

/**
 * Constructs an MP3 from a given file.
 * @param file The file
 */
public MP3(File file) {
    super();
    this.file = file;
    isPaused = false;

    if (!file.exists()) {
        System.err.println("MP3 constructed for non-existent file");
        return;
    }

    try {
        fileFormat = AudioSystem.getAudioFileFormat(file);
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public class MP3Player implements Runnable {

    private double startTime;
    boolean paused;

    public MP3Player(double var) {
        this.startTime = var;
        paused = false;
    }

    public void run() {
        din = null;
        try {
            AudioInputStream in = AudioSystem.getAudioInputStream(file);
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            din = AudioSystem.getAudioInputStream(decodedFormat, in);

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
// ****** This is the line that breaks when SWT is included
            line = (SourceDataLine) javax.sound.sampled.AudioSystem.getLine(info);

            if (line != null) {
                line.open(decodedFormat);
                byte[] data = new byte[4096];

                // Start
                line.start();
                isPlaying = true;

                // Skip first startTime seconds
                int bytesToSkip = (int) (startTime
                        * (Integer) fileFormat.properties().get("mp3.bitrate.nominal.bps") / 8);
                din.skip(bytesToSkip);

                int nBytesRead;
                while ((data != null) && (din != null) && (nBytesRead = din.read(data, 0, data.length)) != -1
                        && isPlaying) {
                    // Skip first startTime seconds
                    if (isPaused == true) {
                        while (isPaused) {
                            Thread.sleep(100);
                        }
                    }
                    line.write(data, 0, nBytesRead);
                    // System.out.println(line.getMicrosecondPosition());
                }

                isPlaying = false;
                // Stop
                if (line != null && din != null) {
                    line.drain();
                    line.stop();
                    line.close();
                    din.close();
                }
            }

        } catch (Exception e) {
            System.out.println("Error!");
            e.printStackTrace();
        } finally {
            if (din != null) {
                try {
                    din.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

@Override
public void play(double startTime) {
    this.startTime = startTime;
    myMP3Player = new MP3Player(startTime);
    MP3Thread = new Thread(myMP3Player);
    MP3Thread.start();

}

public void playthread(double startTime) {

}

@Override
public double getDuration() {
    // TODO Auto-generated method stub
    Long microseconds = (Long) fileFormat.properties().get("duration");
    double milliseconds = (int) (microseconds / 1000);
    double seconds = (milliseconds / 1000);
    return seconds;
}

@Override
public void pause() {
    // TODO Auto-generated method stub
    isPaused = true;
    // MP3Thread.suspend();
    myMP3Player.paused = true;
}

public void unpause() {
    // TODO Auto-generated method stub
    isPaused = false;
    myMP3Player.paused = false;
    // MP3Thread.resume();
}

@Override
public double getCurrentTime() {
    // TODO Auto-generated method stub
    System.out.println(line.getMicrosecondPosition());
    return startTime + (line.getMicrosecondPosition() / 1000);
}

/**
 * Returns the file associated with this MP3
 * @return The mp3 file
 */
public File getFile() {
    return file;
}

public void stop() {
    isPlaying = false;

    pause();
    MP3Thread = null;
    myMP3Player = null;

    isPaused = false;
    isPlaying = false;
    // fileFormat = null;
    MP3Thread = null;
    line = null;
    din = null;
    myMP3Player = null;
    startTime = 0;

    // Stop
    // line.drain();
    // line.stop();
    // line.close();
}

}