如何在javafx项目中读取文件?

tv6aics1  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(623)

这个问题在这里已经有了答案

如何确定我的javafx应用程序所需的fxml文件、css文件、图像和其他资源的正确路径(1个答案)
上个月关门了。
我刚开始学习javafx,所以作为第一个程序,我想编写一个媒体播放器的代码,我在oracle.com上找到了一个示例。该项目有两个java文件:mediacontrol.java和embeddedmediaplayer.java,其中main方法是。当我以复制的方式运行代码时,它工作了。

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);
    }
}

但是当我将提供默认视频的行(就在类embeddedmediaplayer的声明之后)替换为:

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

我得到这些错误:

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

那么,我为什么得到这个?如何正确添加文件“test.mp4”?

vktxenjb

vktxenjb1#

使用此代码

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);
            }
        }
    }
}

从maven导入xuggle-xuggler-5.4.jar

相关问题