gpt4 book ai didi

java - 使用 JavaFX 将视频循环到 JPanel 时的线程

转载 作者:行者123 更新时间:2023-12-02 07:14:25 27 4
gpt4 key购买 nike

我目前正在为学校开展一个项目,我正在尝试创建一个图片库和视频播放器。在欢迎窗口中,我使用 MouseListener 创建了两个 JPanel,一个用于保存图像,另一个用于保存大约 5 秒的循环视频。起初我用的是这个:

public class WelcomeVideoLoader extends JPanel implements Runnable {

Media media;
MediaPlayer player;
MediaView view;
JFXPanel jfxPanel;
Dimension d;
DoubleProperty width;
DoubleProperty height;
Group root;

public WelcomeVideoLoader(File file, Dimension d) {

this.d = d;
media = new Media(file.toURI().toString());

jfxPanel = new JFXPanel();

playAndLoopVideo();

}

private void playAndLoopVideo() {

root = new Group();

player = new MediaPlayer(media);
player.play();
player.setMute(true);
view = new MediaView(player);

DoubleProperty width = view.fitWidthProperty();
DoubleProperty height = view.fitHeightProperty();

width.bind(Bindings.selectDouble(view.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(view.sceneProperty(), "height"));

view.setPreserveRatio(true);
root.getChildren().add(view);
player.setOnEndOfMedia(this);

PlatformImpl.runLater(
new Runnable() {
@Override
public void run() {
Scene scene = new Scene(root, d.getWidth(), d.getHeight());
jfxPanel.setScene(scene);
}
});

super.add(jfxPanel);
}

@Override
public void run() {
player.stop();
playAndLoopVideo();
}
}

这是堆栈跟踪:

Exception in runnable
java.lang.IllegalArgumentException: Group@4d714a77[styleClass=root]is already set as root of another scene
at javafx.scene.Scene$10.invalidated(Scene.java:991)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.Scene.setRoot(Scene.java:953)
at javafx.scene.Scene.<init>(Scene.java:291)
at javafx.scene.Scene.<init>(Scene.java:260)
at se.theshahin.gallery.utils.WelcomeVideoLoader$1.run(WelcomeVideoLoader.java:69)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javafx.embed.swing.JFXPanel$HostContainer$1.run(JFXPanel.java:741)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这可能适用于 6-7 个循环,但每个循环都会出现 IllegalArgumentException,表示该 Group 已在另一个场景中使用。因此,我尝试创建一个 Group 对象的 ArrayList,并创建其中 100 个,看看是否有效。事实并非如此。事实证明,我的线程彼此独立工作,一段时间后它们就变得困惑了。

我对编程还是新手,似乎找不到太多我能理解的主题,所有内容似乎都是为具有多年经验的人编写的。我已经为此工作了 16 个小时,但毫无结果,如果有人能指出正确的方向,我将不胜感激。谢谢。

最佳答案

对于那些偶尔会收到上述异常(exception)情况的人:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javafx.embed.swing.JFXPanel$HostContainer$1.run(JFXPanel.java:741)...

这种情况发生在 Java 7 中,但不会发生在 Java 8 中。传播错误的原因似乎有所不同。

错误来源在 JFXPanel.java 中。在 Java 8 中 JFXPanel.java更改为添加空指针检查:

if (scenePeer != null)

Java 7 decompiled版本(开源不可用)如下所示:

   public void setEmbeddedScene(EmbeddedSceneInterface var1) {
JFXPanel.this.scenePeer = var1;
if(JFXPanel.this.scenePeer != null) {
if(JFXPanel.this.pWidth > 0 && JFXPanel.this.pHeight > 0) {
JFXPanel.this.scenePeer.setSize(JFXPanel.this.pWidth, JFXPanel.this.pHeight);
}

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFXPanel.this.scenePeer.setDragStartListener(JFXPanel.this.dnd.getDragStartListener());
}
});
}
}

JFXPanel class in Java 8通过额外的空检查修复了

public void setEmbeddedScene(EmbeddedSceneInterface embeddedScene) {
scenePeer = embeddedScene;
if (scenePeer == null) {
return;
}
if (pWidth > 0 && pHeight > 0) {
scenePeer.setSize(pWidth, pHeight);
}

// DnD-related calls on 'scenePeer' should go from AWT EDT.
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
if (scenePeer != null) {
scenePeer.setDragStartListener(dnd.getDragStartListener());
}
}
});
}

关于java - 使用 JavaFX 将视频循环到 JPanel 时的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102084/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com