我在这里很难过。我写了一个非常基本的程序来测试我们在一个单独的Eclipse项目中工作的MP3抽象。 MP3类有3个依赖项,我转移到主项目,我们也复制了类。但是,当试图在新项目上播放歌曲时,代码突然停止工作。修复?取下SWT罐子。我无法弄清楚它为什么会起作用,但是删除SWT会使MP3播放,并将其添加回来再打破它。 swt.jar中包含的唯一内容是org.eclipse.swt
代码,通过使用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的线条):
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();
}
}
答案 0 :(得分:0)
我很惊讶如何,但我重新下载了最新的SWT,虽然我必须在启动时添加-XstartOnFirstThread
参数,但我得到了***WARNING: Display must be created on main thread due to Cocoa restrictions.
错误,但它确实有效。