在javafx.scene.media.MediaPlayer.init上使用Media Player时出现Java FX Null Pointer异常 [英] Java FX Null Pointer Exception when using Media Player at javafx.scene.media.MediaPlayer.init

查看:489
本文介绍了在javafx.scene.media.MediaPlayer.init上使用Media Player时出现Java FX Null Pointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

媒体块中没有启动MediaPlayer的空字段

我对此进行了广泛的研究,但没有发现任何解释这个错误。

I have researched this extensively and have found nothing to explain this error.

我正在为一个类项目构建一个Java Swing应用程序,并且我正在尝试使用JFX面板和媒体播放器来托管和播放几个mp4视频。我已经成功地让它工作了一段时间但是当我再次回到窗口时我遇到了错误。

I am building a Java Swing application for a class project and am attempting to use the JFX panel and media player to host and play several mp4 videos. I have successfully got it working one time through however when I come back to the window again I run into the error.


java.lang。 NullPointerException异常处
com.sun.media.jfxmediaimpl.NativeMediaPlayer.setBalance
com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer.playerSetBalance(未知
来源)(未知
来源)位于
的javafx.scene.media.MediaPlayer.init(未知来源)javafx.scene.media.MediaPlayer。(未知来源)
project.screens.TutorialPlayerScreen.initMediaPlayer(TutorialPlayerScreen.java) :156)
at
project.screens.TutorialPlayerScreen.init(TutorialPlayerScreen.java:122)
at
project.screens.TutorialPlayerScreen。(TutorialPlayerScreen.java:113)$ b在
project.screens.PreKModuleSelect.clicked(PreKModuleSelect.java:359)在
$ project.buttons.PreKModuleSelectTutorialButtons $按钮$ 7.doAction b(PreKModuleSelectTutorialButtons.java:225)

at project.tools.ContentPane.notifiyObserver(ContentPane.java:457)
at project.tools.ContentPane $ 1.mousePressed(ContentPane.java:272)at
java.awt.Component。

javax.swing.JComponent.processMouseEvent(未知来源)中的processMouseEvent(未知来源)
的java.awt.Component.processEvent(未知来源)
java.awt.Container.processEvent(来自

java.awt.Component.dispatchEventImpl(未知来源)的未知来源
java.awt.Container.dispatchEventImpl(未知来源)
java.awt.Component.dispatchEvent(未知来源) )处,在
java.awt.LightweightDispatcher.dispatchEvent(来源不明)在
java.awt.LightweightDispatcher.processMouseEvent(来源不明)
java.awt.LightweightDispatcher.retargetMouseEvent(来源不明)
java.awt.Container.dispatchEventImpl(未知来源)
java.awt.Window.dispatchEventImpl(未知来源)
java.awt.Component.dispatchEvent(未知)来自
java.awt.EventQueue.dispatchEventImpl(未知来源)at
java.awt.EventQueue.access $ 500(未知来源)
java.awt.EventQueue $ 3.run(未知)源)在
$ java.awt.EventQueue中3.run(来源不明)在
java.security.AccessController.doPrivileged(本机方法)在
$ java.security.ProtectionDomain 1.doIntersectionPrivilege(未知
来源)at
java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown
Source)at java.awt.EventQueue $ 4.run(Unknown Source)at
java.awt.EventQueue $ 4.run(来源不明)在
java.security.AccessController.doPrivileged(本机方法)在
$ java.security.ProtectionDomain 1.doIntersectionPrivilege(未知
来源)在java.awt.EventQueue中.bispatchEvent(未知来源)at
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at jav a.awt.EventDispatchThread.pumpEventsForHierarchy(未知
来源)在java.awt.EventDispatchThread.pumpEvents在java.awt.EventDispatchThread.pumpEvents(来源不明)
(来源不明)在
的Java。 awt.EventDispatchThread.run(未知来源)

java.lang.NullPointerException at com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer.playerSetBalance(Unknown Source) at com.sun.media.jfxmediaimpl.NativeMediaPlayer.setBalance(Unknown Source) at javafx.scene.media.MediaPlayer.init(Unknown Source) at javafx.scene.media.MediaPlayer.(Unknown Source) at project.screens.TutorialPlayerScreen.initMediaPlayer(TutorialPlayerScreen.java:156) at project.screens.TutorialPlayerScreen.init(TutorialPlayerScreen.java:122) at project.screens.TutorialPlayerScreen.(TutorialPlayerScreen.java:113) at project.buttons.PreKModuleSelectTutorialButtons$Button$7.doAction(PreKModuleSelectTutorialButtons.java:225) at project.screens.PreKModuleSelect.clicked(PreKModuleSelect.java:359) at project.tools.ContentPane.notifiyObserver(ContentPane.java:457) at project.tools.ContentPane$1.mousePressed(ContentPane.java:272) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

下面是我用于启动JFX媒体的代码:

Here is the code I used to start the JFX media:

'//媒体播放器支持
private group mediaRoot;
private Scene mediaScene;

' //Media Player Support private Group mediaRoot; private Scene mediaScene;

private Media tutorialVideo;
private MediaPlayer VideoPlayer;
private MediaView mediaViewer;
private JFXPanel fxPanel;

private int jfxPanelHeight = 394;//525;
private int jfxPanelWidth = 700;//700;

private void initMediaPlayer() throws IOException {
    this.fxPanel = new JFXPanel(); //initializes JFX variables
    fxPanel.setSize(this.jfxPanelWidth ,this.jfxPanelHeight);

    //Add JFX Panel component to the Main Window
    int padding = ((mainWindow.getPreferredSize().width - this.jfxPanelWidth) / 2);
    mainWindow.addLayer(fxPanel, MEDIA_LAYER, padding, 125);

    //Initialize FX Panel
    this.mediaRoot = new Group();
    this.mediaScene = new Scene(this.mediaRoot, 0, 0);

    //Open/prepare the file
    //String tutorialFilePath = new File("").getAbsolutePath() + DIRECTORY_PATH + "Tutorial.mp4";
    String tutorialFilePath = new File("").getAbsolutePath() + MEDIA_PATH + this.observer.getName() +"Tutorial.mp4";
    File mediaFile = new File(tutorialFilePath);
    this.tutorialVideo = new Media(mediaFile.toURI().toString());

    //Create the media player
    this.VideoPlayer = new MediaPlayer(this.tutorialVideo);  //Error here
    this.VideoPlayer.setAutoPlay(false);

    this.mediaViewer = new MediaView(this.VideoPlayer);
    this.mediaViewer.setFitHeight(this.jfxPanelHeight);
    this.mediaViewer.setFitWidth(this.jfxPanelWidth);
          ((Group)this.mediaScene.getRoot()).getChildren().add(this.mediaViewer);

    fxPanel.setScene(this.mediaScene);
}'

我尝试在离开屏幕前清理已用内存。

I attempt to clean up the used memory before leaving the screen.

public void tearDown(){

    //Stop the JFX Player and Remove
    this.mainWindow.removeLayer(this.fxPanel);
    this.VideoPlayer.stop();
    this.VideoPlayer.dispose();
    this.fxPanel.removeAll();
    this.mediaRoot.getChildren().removeAll();

    this.mediaRoot = null;
    this.mediaScene = null;
    this.mediaViewer = null;
    this.tutorialVideo  = null;
    this.VideoPlayer = null;
    this.fxPanel = null;
}

private JLayeredPane contentPane;    //The content pane of this JFrame.
public void removeLayer(JComponent component) {
    contentPane.remove(component);
    contentPane.revalidate();
    contentPane.repaint();
}

任何帮助或评论都将不胜感激!谢谢!

Any help or comment would be much appreciated! Thank you!

推荐答案

您的代码中存在线程问题,需要修复。具体来说,您必须创建FX UI组件并在FX Application线程上设置场景图。请参阅 Javadocs for JFXPanel 了解详细信息。

You have threading issues in your code, which need to be fixed. Specifically, you must create FX UI components and set up the scene graph on the FX Application thread. See the Javadocs for JFXPanel for details.

然而,这是Null Pointer Exception的附带条件。我认为导致这一点的是,当您从UI中完全删除 JFXPanel 时,FX工具包正在关闭。为了防止这种情况,只需调用

This is incidental to the Null Pointer Exception, however. What I think is causing that is that the FX toolkit is closing down when you remove the JFXPanel completely from the UI. In order to prevent this, just call

Platform.setImplicitExit(false);

。 (这种方法可以安全地从任何线程调用。)

when you first initialize your application. (This method is safe to call from any thread.)

这是一个类似的SSCCE:

Here's a similar-looking SSCCE:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class MediaPlayerInSwing {

    private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" ;

    private JFrame window ;

    private JFXPanel jfxPanel ;
    private Media media ;
    private MediaPlayer player ;
    private MediaView mediaView ;
    private BorderPane root ;
    private Scene scene ;

    private Button button ;

    // create on AWT Event Dispatch Thread
    public MediaPlayerInSwing() {
        Platform.setImplicitExit(false);
        initGUI();
    }

    private void initGUI()  {
        window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new BorderLayout());

        JPanel controls = new JPanel();
        controls.setLayout(new FlowLayout());
        button = new Button("Show video");
        controls.add(button);
        button.addActionListener(e -> {
            if (jfxPanel == null) {
                showVideo();
            } else {
                tearDownVideo();
            }
        });

        window.add(controls, BorderLayout.SOUTH);
        window.setSize(600, 480);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

    private void showVideo() {


        jfxPanel = new JFXPanel();

        Platform.runLater(() -> {
            media = new Media(MEDIA_URL);
            player = new MediaPlayer(media);
            player.play(); 
            mediaView = new MediaView(player);
            root = new BorderPane(mediaView);
            scene = new Scene(root, 600, 400);
            jfxPanel.setScene(scene);
        });

        window.add(jfxPanel, BorderLayout.CENTER);
        button.setLabel("Hide video");
    }

    private void tearDownVideo() {

        window.remove(jfxPanel);

        Platform.runLater(() -> {
            player.stop();
            player.dispose();
            player = null ;
            mediaView = null ;
            root = null ;
            jfxPanel.setScene(null);
            scene = null ;
            SwingUtilities.invokeLater(() -> {
                jfxPanel = null ;
                // force window to repaint...
                window.getRootPane().repaint();
            });
        });
        button.setLabel("Show video");
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(MediaPlayerInSwing::new);
    }
}

这篇关于在javafx.scene.media.MediaPlayer.init上使用Media Player时出现Java FX Null Pointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆