JAVAFX媒体:严重的优化问题

时间:2019-07-12 22:43:04

标签: java multithreading javafx optimization

该程序是一个显示媒体的系统:图像,视频并在它们之间保持交替。问题是使用增加的内存:程序运行30分钟后,它消耗了1.2gb的内存

我对我能做的事情并不了解,我相信内存消耗增加的原因是递归(函数调用自身)或每次提供图片都会创建线程的事实,以及视频时,它使用技术上可运行的“正确”(.setOnEndOfMedia()) 请记住,我不能使用计时器/时间轴,因为我的视频具有不同的时长,这种方式可以处理图像

package testevideo2;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TesteVideo2 extends Application{

StackPane stack = new StackPane();

int xImagem = 0;
int xVideo = 0;

public void start(Stage primaryStage) throws Exception {

    //primaryStage.setScene(videoCena);
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    primaryStage.setFullScreen(true);
    primaryStage.setTitle("Titulo bonito");
    primaryStage.show();
    proximo(primaryStage);
    /*player.play();
    player.setOnEndOfMedia(new Runnable() { //Classe Anônima
        @Override
        public void run() {
            primaryStage.setScene(imagemCena);
            //primaryStage.centerOnScreen();
        }
    });*/
}

private void proximo(Stage primaryStage){
    //valores serao pego da api...
    boolean[] eVideo = {false, false, true, false, true};
    String[] nomeImagens = {"doido.jfif", "eu.jpg", "resultado.jpg", "37Teste.jpg"};
    String[] nomeVideos = {"xx.mp4", "carinha.mp4"};
    final String diretorioBase = "file:/C:/Users/Thiago/Desktop/arquivos_projetoandre/";
    if(xImagem + xVideo < eVideo.length){
        //look if the next file is a video or an image
        if(eVideo[xImagem + xVideo]){  
            //criador de video
            Media media = new Media(diretorioBase + nomeVideos[xVideo]);
            MediaPlayer player = new MediaPlayer(media);
            Scene videoCena = new Scene(new Group(new MediaView(player)), 1366, 720);
            videoCena.setCursor(Cursor.NONE);
            player.play();
            player.setOnEndOfMedia(new Runnable() { //Classe Anônima
                @Override
                public void run() {
                    proximo(primaryStage);
                    //primaryStage.centerOnScreen();
                }
            });
            primaryStage.setScene(videoCena);
            xVideo++;
        } else {
                //criador de imagem
                Pane pane = new HBox();
                Image img = new Image(diretorioBase + nomeImagens[xImagem]);
                pane.getChildren().add(new ImageView(img));
                Scene imagemCena = new Scene(pane, 1366, 720);
                //PROBABLY PROBLEM HERE --- CREATE A NEW THREAD ONLY TO WAIT 4 SECONDS
                Thread a = new Thread(new Runnable() { 
                    public void run() { 
                        try {
                            Thread.sleep(4000);
                            //force to the application run on 'javaFx thread'
                            Platform.runLater(new Runnable(){
                                @Override
                                public void run() {
                                    proximo(primaryStage);
                                }
                            });
                        } catch (InterruptedException ex) {
                            Logger.getLogger(TesteVideo2.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });
                a.start();
                primaryStage.setScene(imagemCena);
                xImagem++;
                //Thread.sleep(4000);
                //proximo(primaryStage);
        }
    } else {
        xVideo = 0;
        xImagem = 0;
        proximo(primaryStage);
    }
}


public static void main(String [] args) {
    Application.launch();
}
}

我希望它的功能与现在相同,只是处理的使用随着时间的推移而增加,因为该应用程序将运行数小时...

1 个答案:

答案 0 :(得分:3)

如果您停止使用dispose()对象来释放其所有资源,则需要在其上调用MediaPlayer

还要确保您的Java版本为8或更高(旧版本中存在内存泄漏)。