Reputation: 1219
In my fxml file I have:
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<children>
<Button layoutX="14.0" layoutY="461.0" mnemonicParsing="false" text="Dummy Button" />
<MediaView id="videoView" fitHeight="400.0" fitWidth="450.0" layoutX="14.0" layoutY="14.0" />
</children>
</AnchorPane>
And here is my code:
File f = new File("video.mp4");
Media media = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = (MediaView) videoLayout.lookup("#videoView");
mediaView.setMediaPlayer(mediaPlayer);
Dummy button appears so layout is loaded correctly. There are no exceptions or errors but still nothing happens. I don't see any video frame, nothing is being played. I'm running that code on Windows 7. What am I doing wrong?
EDIT:
Code is ok. Just not all of my test content was supported. As suggested below it's good to check player state:
mediaPlayer.setOnError(()->System.out.println("media error"
+ mediaPlayer.getError().toString()));
I've met 3 issues so far:
Upvotes: 4
Views: 3385
Reputation: 21
In ubuntu 17.10 there was some problem with libavcodec. javafx searches for libavcodec53 but ubuntu 17.10 comes with a later version
you can correct this issue by doing the following:
sudo apt-get install libavcodec57;
sudo apt-get install libavcodec-extra;
sudo apt-get install libavcodec-dev
2.create a symlink
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libavcodec.so libavcodec.so.53
When javafx searches for libavcodec53 now we are pointing to libavcodec57.
Upvotes: 1
Reputation: 1066
Just to help others, writing an answer to this question. This might help. When creating a video player, you should not re initialize the mediaView. If you will re initialize the mediaView then mediaView will loose the reference of originial node. You just need to set a mediaPlayer to your mediaView and check the null reference of mediaView. If the mediaView is null, then instantiate it.
Refer to the following url and look at the answer given by itachi. javafx mediaview only the audio is playing
If you are not using FXML loader to instantiate mediaView, then the following code might work. It does work for me.
private void playVideo(String fileLocation) {
System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
// mediaPlayer.setAutoPlay(true);
if(mediaView == null) {
mediaView = new MediaView(mediaPlayer);
}
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.play();
mediaPlayer.setOnError(() -> System.out.println("Current error: "+mediaPlayer.getError()));
setVideoMediaStatus(PLAYING);
pane.getChildren().add(mediaView);
}
Upvotes: 0
Reputation: 45486
Check that the media is supported:
mediaPlayer.setOnError(()->
System.out.println("media error"+mediaPlayer.getError().toString()));
If there's a MediaException
, like audio format unsupported, you won't see the video, but the application will launch normally.
Note that you could try this other approach, embedding all the code in the FXML file:
<MediaView id="videoView" fitHeight="400.0" fitWidth="450.0" layoutX="14.0" layoutY="14.0">
<mediaPlayer>
<MediaPlayer autoPlay="true">
<media>
<Media source="@video.mp4" />
</media>
</MediaPlayer>
</mediaPlayer>
</MediaView>
Upvotes: 2