-1

I want to create a Movie Player in javafx using eclipse.My code is compile successfully but it gives run time error.I tried using different file path also. But it not resolved the error. My code is

package Player3;

  import javafx.application.Application;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.media.Media;
  import javafx.scene.media.MediaPlayer;
  import javafx.scene.media.MediaView;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;

 public class MediaPlayer3 extends Application{

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

   public void start(Stage stage) throws Exception {
    Group root = new Group();
    Media media =new Media("file:////‪C://Kaise.MP4");
    MediaPlayer player4=new MediaPlayer(media);
    MediaView view = new MediaView(player4);
    root.getChildren().add(view);
     Scene scene =new Scene(root,400,400,Color.BLACK);
     stage.setScene(scene);
     stage.show();
     player4.play();

    }
 }

The error is

        Exception in Application start method
        java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
        java.lang.RuntimeException: Exception in Application start method
         java.lang.RuntimeException: Exception in Application start method
JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
Debu
  • 19
  • 4

1 Answers1

0

As @Sergei Sirik mentioned this is JavaFX and not 'pure' java. Anyway reading the documentation of Media class for the constructor(String source) you will see :

The source must represent a valid URI and is immutable. Only HTTP, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown

So I will suggest first to create a File object ( to be able to check file permissions for read write etc ) and after that just pass the mediaFile.toURI().toString() to the Media class constructor and it will open.

Edit : I guess you will use a FileChooser in future in order to load your video so it will make the file creation and handling much easier.

I had test the code below and it loads successfully my videos on my computer.

import java.io.File;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class Main extends Application {

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

    public void start(Stage stage) throws Exception {
        Group root = new Group();

        FileChooser fc = new FileChooser();

        File x = fc.showOpenDialog(null);

        Media media = new Media(x.toURI().toString());
        MediaPlayer player4 = new MediaPlayer(media);
        MediaView view = new MediaView(player4);
        root.getChildren().add(view);
        Scene scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        player4.play();

    }
}

If you have any error like : MediaException: MEDIA_UNSUPPORTED : Unrecognized file signature! It's probably cause you alter the file signature by hand or try to play an unsupported file format like mkv.

JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
  • The code is ruining now, but it will not playing video. It shows only black screen . – Debu Sep 29 '17 at 21:17
  • Even I used FileChooser also but the problem is same. – Debu Sep 29 '17 at 21:19
  • Check the post again and make sure you and I are running the same code. If the still have a problem let me know. And try to give me some information about the media file you want to load – JKostikiadis Sep 29 '17 at 21:26
  • I tried your coed also,but its having same problem. object name:=>C:\Users\debo\Desktop\Maroon 5 - Sugar.mp4 . Name=>Maroon 5 - Sugar.mp4 . Type of file :=> VLC media file (.mp4) . File location:=>C:\Users\debo\Desktop. – Debu Sep 30 '17 at 08:09
  • Have you tried any other media file? If no give it a shot. Maybe your video file is corrupted or something like that.If you can't play any video maybe there is something with your java version? hmm i guess no... – JKostikiadis Sep 30 '17 at 09:50
  • I already tried different file. Me also think so ,its having java version problem – Debu Sep 30 '17 at 11:26
  • Take a look here : https://stackoverflow.com/questions/20597998/javafx-video-not-playing You might miss a windows media package which could cause the problem, at least thats what the above post it about. – JKostikiadis Sep 30 '17 at 22:34