你好我试图在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);
答案 0 :(得分:2)
我还没有尝试过使用MediaPlayer,但是Javadoc声明它的构造函数将Media对象作为参数。 Media对象的构造函数采用URL 字符串。所以我怀疑你需要做类似以下的事情:
Media media = new Media(getClass().getClassLoader()
.getResource("/StartPageVideo.mp4").toString());
MediaPlayer mp = new MediaPlayer(media);
答案 1 :(得分: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);
试着这个希望为你工作..