javafx.scene.media.MediaView无法强制转换为javafx.scene.Parent

时间:2014-10-01 18:27:52

标签: java video javafx media

我有一个MediaView,我想要播放一个全屏视频..我无法找到我MediaView的正确父级..它给了我这个错误 javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

这是我的fxml文件

<?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" />

那是我的控制器

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

    }

}

1 个答案:

答案 0 :(得分:0)

您不得将MediaView用作根节点,因为它不会从Parent延伸。尝试将MediaView包裹在Pane内。例如:

<?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>

N.B。 您可能还想为MediaViewMediaPlayer定义不同的引用名称。在上面的示例中,您已定义fx:id MediaView并将MediaPlayer引用为astrolabe_intro

MediaView/MediaPlayer is hereFXML is here

的简单示例