-3

I am trying a build a small video player using java I am getting some errors,please help me fix them.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Player player = new Player("/Users/name/Desktop/play.mp4");
        Scene scene = new Scene(player, 720,480,Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

package sample;

import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

/**
 * Created by akanksh on 03/11/17.
 */
public class Player extends BorderPane {
    Media media;
    MediaPlayer player;
    MediaView view;
    Pane mpane;
    public Player(String file){

        media = new Media(file);
        player = new MediaPlayer(media);
        view = new MediaView(player);
        mpane = new Pane();

        mpane.getChildren().add(view);

        setCenter(mpane);
        player.play();

    }
}
errors :

enter image description here

No matter how many times I tried using different videos and different paths,its not working...help needed...

SedJ601
  • 12,173
  • 3
  • 41
  • 59

2 Answers2

1

The media class needs a valid URI

Therefore you need a "schema" for your file such as file:///Users/...

Or you could use new File("/Users/name/Desktop/play.mp4").toURI()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

The media constructor is looking for a URI, not a file path. If you are really specifying a file for the media (i.e. something the user specified on the file system, for example via a FileChooser) you should convert the path to a valid URI that both has a scheme and properly encodes any invalid characters in the path, such as whitespace.

If you have a File object, you simply do this with

File file = ... ;
Media media = new Media(file.toURI().toString());

If the file is specified as a string, create a file object first (though in any realistic situation you should have a File to begin with):

public Player(String file){

    media = new Media(new File(file).toURI().toString());
    player = new MediaPlayer(media);
    view = new MediaView(player);
    mpane = new Pane();

    mpane.getChildren().add(view);

    setCenter(mpane);
    player.play();

}

Note that in the case where the media is part of the application (instead of being provided on the user's filesystem at run time), you should use a different technique entirely and treat it as a resource. See, for example, How to reference javafx fxml files in resource folder? for accessing resources in JavaFX.

James_D
  • 201,275
  • 16
  • 291
  • 322