JavaFx OnError MediaView内存泄漏

时间:2018-08-14 12:47:25

标签: memory javafx mediaview

我正在使用的软件使用JavaFx MediaPlayers和MediaViews创建UI。为此,我们要重用MediaPlayers(将它们存储在静态类的hashMap中之后),然后在需要时将MediaPlayer放在新的MediaViews中。完成MediaView后,我们将其设置为null并继续运行,但这会导致内存泄漏,其中播放器的数量将保持不变,但mediaViews将增加。我制作了此代码的最低限度工作版本,因此您可以看到内存在不断增加,而无需收集任何mediaViews。看来MediaPlayer的getOnError或Error属性正在保留MediaView的旧引用。我想删除它,但是如果您想清理所有的内存,就好像必须丢弃mediaPlayer一样,但是我想保存Player并删除View。

这里有一些代码可以重新创建问题。只需按一下“停止”按钮几次,即可删除旧的mediaView并添加一个新的mediaView,但没有一个被清理。

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();
    }
}

0 个答案:

没有答案