java - 我如何在 Javafx 项目中读取文件?
问题描述
我刚开始学习 Javafx,所以作为第一个程序,我想编写一个媒体播放器,我在 oracle.com 上找到了一个示例。该项目有两个 java 文件:MediaControl.java 和 EmbeddedMediaPlayer.java,其中主要方法是。当我运行复制的代码时,它起作用了。
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”?
解决方案
使用此代码
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
推荐阅读
- php - 最低电话号码位数 Woocommerce Checkout
- arrays - 我对 Fortran 代码有一个奇怪的问题
- java - 如何为框架设置彩色背景?
- ios - iOS 工具栏 barbuttonitem spacer
- python-3.x - 编写天文/天体动力学程序时遇到的问题
- docker - docker:关于 docker 图像格式和图层的建议
- azure-cosmosdb - 如何在 Azure 搜索数据源中传递 SP
- html - 使用 flexbox 在每个旁边添加多个左侧和右侧边栏
- java - 在线判断给出运行时错误(NZEC)
- mercurial - hg 状态返回空白但文件已更改