gpt4 book ai didi

JAVAFX媒体: Serious optimization problem

转载 作者:行者123 更新时间:2023-12-01 19:37:17 27 4
gpt4 key购买 nike

该程序是一个显示媒体的系统:图像、视频并在它们之间不断交替。问题是使用增加内存:编程运行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();
}
}

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

最佳答案

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

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

关于JAVAFX媒体: Serious optimization problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014752/

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