0

I hope you to help me to resolve this problem , I try make this code for play video from an Button Open like option file in all the program but it doesn't work. I don't know what's wrong.

This is my code:

btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            FileChooser chooser = new FileChooser();
            FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("select your media(*.mp4)", "*.mp4");
            chooser.getExtensionFilters().add(filter);
            File file = chooser.showOpenDialog(primaryStage);
            if ( file !=null){

                Media source = new Media(file.getPath());
                MediaPlayer player = new MediaPlayer(source);
                MediaView view = new MediaView(player);
                root.getChildren().add(view);
                player.play();


            }
             else {
                label.setText("vide ");
            }
fabian
  • 80,457
  • 12
  • 86
  • 114
abdelati
  • 1
  • 1
  • You should append `file.getPath()` with `file://` i.e. `Media source = new Media("file://" + file.getPath());` – ItachiUchiha Jan 30 '16 at 16:16
  • @ItachiUchiha: Seriously? You do it yourself, even though `File` has a method for this? Try doing it your way with a file that has a space in it's name and you get a exception... – fabian Jan 30 '16 at 17:28
  • @fabian I posted the first thing that came to me. I will accept that the answer posted by you is an appropriate solution. – ItachiUchiha Jan 30 '16 at 17:40

1 Answers1

0

You pass the path to the constructor of Media. This constructor expects a URI string however. Fortunately File can be converted to URI using toURI:

Media source = new Media(file.toURI().toString());
fabian
  • 80,457
  • 12
  • 86
  • 114