Memory JavaFx OneError MediaView内存泄漏
我正在开发的软件使用JavaFXMediaPlayer和MediaView创建UI。为了按照我们希望的方式工作,我们正在重用MediaPlayer(将它们存储在静态类的hashMap中之后),然后在需要时将MediaPlayer放置在新的MediaView中。完成MediaView后,我们将其设置为null并继续,但这会导致内存泄漏,玩家数量将保持不变,但MediaView将增加。我制作了这段代码的最小工作版本,这样您就可以看到内存不断增加,而无需收集任何MediaView。看起来MediaPlayer的GetOneError或Error属性保留了MediaView的旧引用。我想删除这个,但是如果你想清理mediaPlayer的所有内存,似乎你必须处理它,但是我想保存播放器并删除视图 下面是一些重新创建问题的代码。只需点击停止按钮几次,即可删除旧的mediaView并添加新的mediaView,但没有一个会被清除Memory JavaFx OneError MediaView内存泄漏,memory,javafx,mediaview,Memory,Javafx,Mediaview,我正在开发的软件使用JavaFXMediaPlayer和MediaView创建UI。为了按照我们希望的方式工作,我们正在重用MediaPlayer(将它们存储在静态类的hashMap中之后),然后在需要时将MediaPlayer放置在新的MediaView中。完成MediaView后,我们将其设置为null并继续,但这会导致内存泄漏,玩家数量将保持不变,但MediaView将增加。我制作了这段代码的最小工作版本,这样您就可以看到内存不断增加,而无需收集任何MediaView。看起来MediaPl
package JavaFx;
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
public class FxMediaExample1 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
public MediaView mediaView;
public Scene scene;
public VBox root;
@Override
public void start(Stage stage)
{
// Locate the media content in the CLASSPATH
// Create a Media
File file = new File("file:///C:/Users/Samuel%20Johnston/Downloads/cf86c6a4-271f-4dcb-bb11-fc30f5eb6b45_web.mp4");
if (file.exists()) {
System.out.println("Boobs");
} else {
System.out.println("poop");
}
Media media = new Media("file:///C:/Users/Samuel%20Johnston/Downloads/cf86c6a4-271f-4dcb-bb11-fc30f5eb6b45_web.mp4");
media.setOnError(() -> {
System.out.println("WHAT THE WHAT, " + media.getError().toString());
System.out.println("WHAT THE WHAT, " + media.getError().getMessage());
System.out.println("WHAT THE WHAT, " + media.getError().getStackTrace().toString());
System.out.println("WHAT THE WHAT, " + media.getError().getLocalizedMessage());
});
// Create a Media Player
final MediaPlayer player = new MediaPlayer(media);
player.setOnError(() -> {
System.out.println("WHY THE WHY, " + player.getError().toString());
System.out.println("WHY THE WHY, " + player.getError().getMessage());
System.out.println("WHY THE WHY, " + player.getError().getStackTrace().toString());
System.out.println("WHY THE WHY, " + player.getError().getLocalizedMessage());
});
// Automatically begin the playback
player.setAutoPlay(true);
// Create a 400X300 MediaView
mediaView = new MediaView(player);
mediaView.setFitWidth(700);
mediaView.setFitHeight(700);
mediaView.setSmooth(true);
// Create the Buttons
Button playButton = new Button("Play");
Button stopButton = new Button("Stop");
// Create the Event Handlers for the Button
playButton.setOnAction(new EventHandler <ActionEvent>()
{
public void handle(ActionEvent event)
{
if (player.getStatus() == Status.PLAYING)
{
player.stop();
player.play();
}
else
{
player.play();
}
}
});
stopButton.setOnAction(new EventHandler <ActionEvent>()
{
public void handle(ActionEvent event)
{
player.stop();
root.getChildren().remove(mediaView);
mediaView = new MediaView(player);
mediaView.setFitWidth(700);
mediaView.setFitHeight(700);
mediaView.setSmooth(true);
root.getChildren().add(mediaView);
}
});
// Create the HBox
HBox controlBox = new HBox(5, playButton, stopButton);
// Create the VBox
root = new VBox(5,mediaView,controlBox);
// Set the Style-properties of the HBox
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Create the Scene
scene = new Scene(root);
// Add the scene to the Stage
stage.setScene(scene);
// Set the title of the Stage
stage.setTitle("A simple Media Example");
// Display the Stage
stage.show();
}
}
packagejavafx;
导入java.io.File;
导入java.net.URL;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.effect.DropShadow;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.scene.media.media;
导入javafx.scene.media.MediaPlayer;
导入javafx.scene.media.MediaPlayer.Status;
导入javafx.scene.media.MediaView;
导入javafx.stage.stage;
导入javafx.scene.paint.Color;
公共类FxMediaExample1扩展了应用程序
{
公共静态void main(字符串[]args)
{
应用程序启动(args);
}
公共媒体观;
公共现场;
公共VBox根;
@凌驾
公众假期开始(阶段)
{
//在类路径中找到媒体内容
//创建媒体
文件=新文件(“file:///C:/Users/Samuel%20Johnston/Downloads/cf86c6a4-271f-4dcb-bb11-fc30f5eb6b45_web.mp4”);
if(file.exists()){
System.out.println(“胸部”);
}否则{
系统输出打印项次(“poop”);
}
媒体=新媒体(“file:///C:/Users/Samuel%20Johnston/Downloads/cf86c6a4-271f-4dcb-bb11-fc30f5eb6b45_web.mp4”);
媒体设置错误(()->{
System.out.println(“什么是什么,”+media.getError().toString());
System.out.println(“什么是什么,”+media.getError().getMessage());
System.out.println(“什么是什么,”+media.getError().getStackTrace().toString());
System.out.println(“什么是什么,”+media.getError().getLocalizedMessage());
});
//创建媒体播放器
最终媒体播放器=新媒体播放器(媒体);
player.setOnError(()->{
System.out.println(“为什么,+player.getError().toString());
System.out.println(“为什么,+player.getError().getMessage());
System.out.println(“为什么,”+player.getError().getStackTrace().toString());
System.out.println(“为什么,”+player.getError().getLocalizedMessage());
});
//自动开始播放
player.setAutoPlay(true);
//创建一个400X300 MediaView
mediaView=新的mediaView(播放器);
mediaView.setFitWidth(700);
mediaView.setFitHeight(700);
mediaView.setSmooth(true);
//创建按钮
按钮播放按钮=新按钮(“播放”);
按钮停止按钮=新按钮(“停止”);
//为按钮创建事件处理程序
playButton.setOnAction(新的EventHandler()
{
公共无效句柄(ActionEvent事件)
{
if(player.getStatus()==Status.PLAYING)
{
player.stop();
player.play();
}
其他的
{
player.play();
}
}
});
setOnAction(新的EventHandler()
{
公共无效句柄(ActionEvent事件)
{
player.stop();
root.getChildren().remove(mediaView);
mediaView=新的mediaView(播放器);
mediaView.setFitWidth(700);
mediaView.setFitHeight(700);
mediaView.setSmooth(true);
root.getChildren().add(mediaView);
}
});
//创建HBox
HBox控制盒=新的HBox(5,播放按钮,停止按钮);
//创建VBox
root=新的VBox(5,mediaView,controlBox);
//设置HBox的样式属性
root.setStyle(“-fx padding:10;”+
“-fx边框样式:内部实心;”+
“-fx边框宽度:2;”+
“-fx边框插图:5;”+
“-fx边框半径:5;”+
“-fx边框颜色:蓝色;”;
//创建场景
场景=新场景(根);
//将场景添加到舞台
舞台场景;
//设定舞台的标题
stage.setTitle(“一个简单的媒体示例”);
//展示舞台
stage.show();
}
}
为什么需要创建新的MediaView
s?为什么不使用单一视图,只更改它显示的MediaPlayer
?我们在播放列表中显示了多张幻灯片。幻灯片中的每个视频都有自己的MediaView,播放列表可以足够大,我们需要动态加载和删除幻灯片。在真实的例子中,我们将有多个不同的视频循环,而不保证幻灯片甚至有视频。测试示例忽略了这一点,因为如果没有它,它不会导致泄漏。您仍然可以有一个或有限多个MediaVie