JavaFX- MediaView not displaying the video - java

I have a MediaView on the scene in which I am trying to play the selected video. The video is playing, I can hear the sound, but it's not visible.
here's my code:
playLocalVideo.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File fileToPlay = null;
//DirectoryChooser directoryChooser = new DirectoryChooser();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Files to Upload");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("VIDEO files (*.mp4)", "*.mp4");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
try {
fileToPlay = fileChooser.showOpenDialog(stage).getCanonicalFile();
Media m = new Media(fileToPlay.toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
videoPlayer = new MediaView(mp);
videoPlayer.setPreserveRatio(true);
mp.play();
} catch (IOException ex) {
Logger.getLogger(TutorControlPanelController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

Sorted:
instead of:
videoPlayer = new MediaView(mp);
You should do:
videoPlayer.setMediaPlayer(mp);

I had the same issue that audio is playing but no video is playing when MP4 file used code MPEG-4. After I changed the codec to H.264 the video became playing correct.

Related

How to open a PDF file javafx

I want to open a pdf file and display it on new window when a button is clicked
i try this an it is not working:
Button btn = new Button();
File file=new File("Desktop/Test.pdf");
btn.setText("Open");
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(Exemple.class.getName())
.log(Level.SEVERE, null, ex);
}
}
});
You can try this way to open a PDF file:
File file = new File("C:/Users/YourUsername/Desktop/Test.pdf");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());
If you want to use FileChooser, then use this:
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
FileChooser fileChooser = new FileChooser();
// Set Initial Directory to Desktop
fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "\\Desktop"));
// Set extension filter, only PDF files will be shown
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF files (*.pdf)", "*.pdf");
fileChooser.getExtensionFilters().add(extFilter);
// Show open file dialog
File file = fileChooser.showOpenDialog(primaryStage);
//Open PDF file
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());
}
});
If you are using windows you need to fix the path of the file like this:
File file=new File("C:\\Users\\USER\\Desktop\\Test.pdf");
You need to change USER with your windows user.
Also, note that \ is used for escape sequences in programming languages.

Using a variable outside of an lambda expression block in JavaFX

So, I have a program that loads an image with a FileChooser. I use a lambda expression to define this loading action so the function LoadImage() returns the chosen image inside the event block but I can't use it outside this block.
Can someone help me?
Here is my code so far:
public class Main extends Application{
Stage window;
BorderPane layout;
ImageView imageView;
Image currentImage;
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
window.setTitle("Image Viewer v.1.0");
//MENU
Menu fileMenu = new Menu("_File");
MenuItem openFile = new MenuItem("Open file...");
openFile.setOnAction(e -> currentImage = LoadImage());
fileMenu.getItems().add(openFile);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
imageView = new ImageView();
imageView.setImage(currentImage); //here the currentImage is null
layout = new BorderPane();
layout.setTop(menuBar);
layout.setCenter(imageView);
Scene scene = new Scene(layout, 900, 600);
window.setScene(scene);
window.show();
}
public Image LoadImage()
{
final Image image;
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
return new Image(file.toURI().toString());
}
} // end of Main class
So forth, nothing is displayed in the ImageView.
How can I use the value returned by the LoadImage() method?
The value of currentImage is indeed changed, but when you set the value of imageView's image property, it is not yet set. You have to change the property of your ImageView, not the variable (which you use only before setting the image).
Try changing your lambda to:
openFile.setOnAction(e -> imageView.setImage(LoadImage()) )

JavaFX MediaPlayer just stops after 1 minute+

I'm using the MediaPlayer class in JavaFX to run the media the whole time untill you close the application but the MediaPlayer just stops after 1 minute and the file is 11 minutes long. This is my code:
#Override
public void start(Stage primaryStage) throws Exception {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(primaryStage);
Media media = null;
if(file != null) {
media = new Media(file.toURI().toString());
}
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaPlayer.play();
Group root = new Group();
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
Hotfix, try adding the following code inside start()
primaryStage.setOnCloseRequest(windowEvent -> {
mediaPlayer.stop();
});

how to insert a picture using file chooser

I'm creating a program to register employees, and was wondering how to put a picture of the employees registered When do your record. I would use a button to select this file and attach the photo along with your registration? But how would this be done using JavaFX? Thank you!!
I managed as follows:
#FXML
private void searchPicture(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
File file = fileChooser.showOpenDialog(new Stage());
if (file != null) {
Image img = new Image(file.toURI().toString());
imagem.setImage(img);
imagem.setFitWidth(171);
imagem.setFitHeight(176);
imagem.setPreserveRatio(false);
}
}
}

Open, edit and save image canvas on file.pgm in JavaFX

this drawing area to draw image with canvas
Canvas canvas= new Canvas();
canvas.setHeight(500);
canvas.setWidth(700);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
and I made this method to export drawing canvas to imagem.pgn or snapshot and its correctly
public void snapshotCanvasImageToPNG(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
WritableImage writableImage = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and I have many doubts in making these methods to open edit and save the canvas drawing to file.pgm
I made this but it complicate to finish
public void openFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
//doubt here
}
}
public void saveFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
//doubt here
}
}
The only way you will get to work with PGM images in JavaFX is through ImageJ or Java Advanced Imaging as mentioned by #VGR or another third party plugin.
Please check this plugin that contains code for such IO functions.
And you might also want to look at this answer which can show you the formats you can easily handle with JavaFX.

Categories

Resources