1

I just started to learn Javafx so as a first program, i wanted to code a Media Player and i found a sample at oracle.com. The Project has two java files : MediaControl.java and EmbeddedMediaPlayer.java where the main method is. When i ran the code as copied it worked.

package embeddedmediaplayer;

import java.io.File;
import java.net.URI;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class EmbeddedMediaPlayer extends Application {
     private static final String MEDIA_URL =
 "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";

    
    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle("Embedded Media Player");
        Group root = new Group();
        Scene scene = new Scene(root, 540, 241);

        // create media player
        Media media = new Media (MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        
        MediaControl mediaControl = new MediaControl(mediaPlayer);
        scene.setRoot(mediaControl);

        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
        
    }
    public static void main(String[] args) {
        launch(args);
    }
}

But when i replace the lines which provide the default video (just after the declaration of the class EmbeddedMediaPlayer) by :

File f = new File("test.mp4");
    URI u = f.toURI();
    private static final String MEDIA_URL = "u";

i get these errors :

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'u'
    at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
    at javafx.scene.media.Media.<init>(Media.java:393)
    at embeddedmediaplayer.EmbeddedMediaPlayer.start(EmbeddedMediaPlayer.java:35)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application embeddedmediaplayer.EmbeddedMediaPlayer
Java Result: 1

So, why did i get this? And how can i correctly add the file "test.mp4"?

ralietonle
  • 11
  • 1
  • please edit your question and move the replaced code into the example (comment the original lines, so we can easily see the difference) - anyway, this seems to be unrelated to javafx, looks like a plain java error: so try the failing lines in a simple java main (no ui), fix as needed and then move them back into the ui :) – kleopatra Nov 29 '20 at 15:43
  • Is the file something the user chooses from their file system, or is it part of the project (ie a resource)? For the latter, see https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Nov 29 '20 at 15:50
  • James, yeah it's a file from the file system but i added it to the project then i could use relative path. – ralietonle Nov 29 '20 at 17:45
  • @ralietonle The question I was asking is whether the file is part of the application (so you would include it with the application bundle/jar file) or whether it’s external to it. – James_D Nov 29 '20 at 18:49
  • I found an answer at the link you gave so thank you! (The file was external). – ralietonle Nov 29 '20 at 19:09

1 Answers1

-2

Use this code

public class VideoPlayer {
    public static void main(String[] args) throws InterruptedException {
        String fileName = "saved.mp4";
        VideoPlayer videoPlayer = new VideoPlayer();
        videoPlayer.play(fileName);
    }

    private void play(String sourceUrl) throws InterruptedException {
        final MyVideoFrame frame = new MyVideoFrame();
        frame.setSize(new Dimension(500, 500));


        IMediaReader reader = ToolFactory.makeReader(sourceUrl);
        reader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);


        MediaListenerAdapter adapter = new MediaListenerAdapter()
        {
            @Override
            public void onVideoPicture(IVideoPictureEvent event)
            {
                frame.setImage(event.getImage());
            }
        };
        reader.addListener(adapter);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.pack();
        frame.setVisible(true);

        while (reader.readPacket() == null)
            do {
                Thread.sleep(100);
            } while(false);
    }

    private class MyVideoFrame extends JFrame{
        public MyVideoFrame() throws HeadlessException {
            //setSize(500, 500);
        }

        Image image;

        public void setImage(final Image image) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    MyVideoFrame.this.image = image;
                    repaint();
                }
            });
        }

        @Override
        public synchronized void paint(Graphics g) {
            if (image != null) {
                g.drawImage(image, 0, 0, null);
            }
        }
    }
}

Import xuggle-xuggler-5.4.jar from maven

Ranjit Vamadevan
  • 514
  • 5
  • 21