简体   繁体   中英

javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

I have a MediaView and I want to play a fullscreen video .. i just can't find the right parent to my MediaView .. it gives me this error javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

that's my fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.layout.AnchorPane?>


<MediaView fx:id="astrolabe_intro" fitHeight="200.0" fitWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="astrolabe.astrolabe_introController" />

and that's my controller

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class astrolabe_introController implements Initializable {

    String workingDir = System.getProperty("user.dir");
    File video = new File(workingDir, "src/astrolabe/img/astrolab_movie.mp4");
    Media m = new Media(video.toURI().toString());
    MediaPlayer astrolabe_intro = new MediaPlayer(m);


    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

        astrolabe_intro.play();

    }

}

You must not use MediaView as the root node, because it doesn't extend from Parent . Try to wrap your MediaView inside a Pane . For example :

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane fx:id="anchorPane" fitHeight="200.0" fitWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="astrolabe.astrolabe_introController" >
    <MediaView fx:id="astrolabe_intro"/>
</AnchorPane>

NB You might also want to define different reference name for your MediaView and MediaPlayer . In the above example you have defined fx:id of MediaView and ref for MediaPlayer as astrolabe_intro

A simple example of MediaView/MediaPlayer is here and FXML is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM