0

您好我正在尝试在 JavaFx 中添加媒体我尝试了许多解决方案已经在堆栈溢出时可用,但它们似乎都不适合我,当我将文件放入 src 文件夹时我尝试了这个:

Media media = new Media("StartPageVideo.mp4");

// Create the player for playing media.
MediaPlayer mediaPlayer = new MediaPlayer(media);
//set player property to autoplay
mediaPlayer.setAutoPlay(true);

// Create the view and add it to the Scene.
MediaView mediaView = new MediaView(mediaPlayer);
Ap_MediaContainer.getChildren().add(mediaView);

当我将视频放入视频包时,我也尝试过:

Media media = new Media("/Video/StartPageVideo.mp4");

    // Create the player for playing media.
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    //set player property to autoplay
    mediaPlayer.setAutoPlay(true);

    // Create the view and add it to the Scene.
    MediaView mediaView = new MediaView(mediaPlayer);
    Ap_MediaContainer.getChildren().add(mediaView);
4

2 回答 2

2

我还没有尝试使用 MediaPlayer,但 Javadoc 声明它的构造函数将 Media 对象作为参数。Media 对象的构造函数采用 URL字符串。因此,我怀疑您需要执行以下操作:

Media media = new Media(getClass().getClassLoader()
             .getResource("/StartPageVideo.mp4").toString());
MediaPlayer mp = new MediaPlayer(media);
于 2013-06-28T13:39:15.550 回答
1

我正在使用此代码,它对我有用。

private static File file=new File("e:/videos/test.mp4");

private static final String source=file.toURI().toString();

Media media = new Media(source);

MediaPlayer mediaPlayer = new MediaPlayer(media);

试试这个希望对你有用..

于 2013-09-26T12:00:30.537 回答