這是我在評論中談到了(創建的臨時文件,並從罐子保存Flash動畫的話)的方式
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.docuverse.swt.flash.FlashPlayer;
public class SWTFlashPlayer {
private FlashPlayer player = null;
private final String FLASH_FILE_PATH = "/EdnCateDance.swf";
private final String TMP_FILE_PREFFIX = "tmp_";
private final String TMP_FILE_SUFFIX = ".swf";
private File swfFile = null;
public SWTFlashPlayer() {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
try {
swfFile = copyFileFromJar(FLASH_FILE_PATH, TMP_FILE_PREFFIX, TMP_FILE_SUFFIX);
} catch (IOException e) {
e.printStackTrace();
return;
}
player = new FlashPlayer(shell, SWT.NONE);
player.loadMovie(0, swfFile.getAbsolutePath());
player.setSize(150, 150);
player.activate();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* Copy file packed inside current jar to temp file
* @param jarPath String - path inside jar
* @param filePreffix String - temp file preffix
* @param fileSuffix - temp file suffix
* @throws IOException - temp file cannot be created or writing somehow fails
*/
private File copyFileFromJar(String jarPath, String filePreffix, String fileSuffix) throws IOException {
File toFile = File.createTempFile(filePreffix, fileSuffix);
// delete file after application finishes
toFile.deleteOnExit();
if(!toFile.canWrite()) throw new IOException("File (" + toFile.getPath() + ") not exists or is not writable!");
FileOutputStream fos = new FileOutputStream(toFile);
InputStream is = this.getClass().getResourceAsStream(jarPath);
if(is == null) throw new IOException("File on jar path could not be located or loaded!");
int read = 0;
byte bytes[] = new byte[1024];
while ((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.flush();
fos.close();
return toFile;
}
public static void main(String[] args) {
new SWTFlashPlayer();
}
}
我用EdnCateDance.swf閃存文件,該文件是在SWT閃光的例子之一圖書館。
如果SWT FlashPlayer無法從流中加載電影,則必須從您從jar中獲取的流中創建臨時文件並由播放器加載它。 – Sorceror
那麼流輸出「file:// C://Desktop//application.jar!//blah.swf「,你可以看到它試圖在應用程序內搜索。我想出了一個真正的骯髒的解決方案。我替換了「application.jar!」我在桌面上創建的名爲「SWF」的文件夾的名稱。問題是,如果應用程序和SWF文件夾位於兩個不同的目錄中,它將無法找到SWF的路徑。但請詳細說明您的評論,我對此很困惑。 – Jman
你可以發佈鏈接到'com.docuverse.swt.flash.FlashPlayer' jar文件嗎?我找不到該項目的任何現有網站。然後我會嘗試製作[SSCCE](http://sscce.org/)。 – Sorceror