JFX会在一段时间后锁定

时间:2019-04-16 21:16:10

标签: java

java版本“ 1.8.0_181” JFX,使用JFX制作了视频课程。有一个小的测试程序来播放视频。测试程序将Jpanel提供给video类,然后循环调用video来播放视频。 (3 mp4大小为5-13兆。)堆设置为100兆。一个小时或更长时间后,程序将锁定。没有崩溃消息,似乎有很多可用堆。 (超过一半。)我应该在哪里寻找问题的地方?

不知道问题出在哪里,我不知道尝试什么。

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;

import javax.swing.JPanel;
import javafx.application.Platform;
//import javafx.beans.binding.Bindings;
//import javafx.beans.property.DoubleProperty;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import com.zowie3.MainBase;

public class Video
{
    final static boolean DO_VIDEO = true;
    final static boolean STRETCH_VID = true;

    static JFXPanel VFXPanel;       // For videos
    static JPanel   videoPanel;     // VFXPanel added here

    File video_source;
    MediaPlayer player;
    MediaView   viewer;
    Media media; 
    StackPane root;
    Scene scene;
    Rectangle rect; 

    Button b;
    boolean videoDone;
    boolean jfxDone;    

    //-- Once at startup --
    static public void init()
    {
        if (DO_VIDEO)
        {
            VFXPanel = new JFXPanel();
            VFXPanel.setBackground(Color.black);

            videoPanel = MainBase.getPanelForVideo();
            videoPanel.setLayout(new BorderLayout());
            videoPanel.add(VFXPanel, BorderLayout.CENTER);
            videoPanel.setBackground(Color.black);
            videoPanel.setVisible(false);
        }
    }

    //-- Call to play a video --
    public void playVideo(String vid)
    { 
        if (DO_VIDEO)
        {
            videoDone = false;

            video_source = new File(vid);
            try
            {
                media = new Media(video_source.toURI().toString());
            }
            catch (Exception e)
            {
                return; //Media fail return - TODO Perhaps some info returned?
            }       

            Platform.runLater(new Runnable() 
            {
                @Override
                public void run() 
                {
                    initFX();
                }
            });          

            //-- Block until video is done --
            while (videoDone == false) 
            {    
                try
                {
                    Thread.sleep(250);  //500 1000
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            //-- JFX will run --
            jfxDone = false;
            Platform.runLater(new Runnable() 
            {
                @Override
                public void run() 
                {                
                    endFX();
                }
            });     

            //-- Block tlil JFX is done --
            while (jfxDone == false) 
            {    
                try
                {
                    Thread.sleep(500);  //1000
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }       

            MainBase._instance.setFocusable(true);
            MainBase._instance.toFront();      
        } 
    } 

    synchronized void endFX()
    { 
        b.removeEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);        
        root.getChildren().clear();

        scene = null;
        root = null;        
        player.dispose();
        player = null;        
        media = null;
        video_source = null;
        VFXPanel.setVisible(false);
        videoPanel.setVisible(false);

        jfxDone = true;
    }    

    //-- Mouse event handler -- 
    EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() 
    { 
       @Override 
       public void handle(MouseEvent me) 
       { 
          me.consume();

        //-- JFX will run --
          Platform.runLater(new Runnable() 
          {
              @Override
              public void run() 
              {
                  player.seek(Duration.INDEFINITE);
              }
          });    
       }
    };   

    synchronized void initFX()
    { 
        player = new MediaPlayer(media);
        player.setOnEndOfMedia(new Runnable() 
        {
           synchronized public void run() 
           {
               videoDone = true;               
           }
        });            

        viewer = new MediaView(player);

        //-- resize video based on screen size --
        if ( STRETCH_VID )
        {
            viewer.setFitHeight(MainBase.SCREENH+50);
            viewer.setFitWidth(MainBase.SCREENW+50);
            /*
            DoubleProperty width = viewer.fitWidthProperty();
            DoubleProperty height = viewer.fitHeightProperty();
            width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
            height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
            viewer.setPreserveRatio(true);
            */          
        }

        root = new StackPane();
        scene = new Scene(root);

        //-- Black backdrop --
        rect = new Rectangle(0,0, MainBase.SCREENH, MainBase.SCREENH); // Scene Rect
        rect.setFill(Paint.valueOf("BLUE"));   //BLACK final, BLUE to see panel size on screen
        root.getChildren().add(rect);

        root.getChildren().add(viewer);

        //-- Transparent Abort Button --
        b = new Button("abort");
        b.setMinWidth(VFXPanel.getWidth());
        b.setMinHeight(VFXPanel.getHeight());
        b.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
        b.setOpacity(0);
        root.getChildren().add(b);          

        VFXPanel.setScene(scene);
        VFXPanel.setVisible(true);
        videoPanel.setVisible(true); 
        player.setAutoPlay(true);
    }   
}

反复播放相同的3个短视频。有了100meg的堆,我希望它可以一直运行直到停止。但是一个小时左右就会冻结。没有错误。

0 个答案:

没有答案