java 在 JavaFX8 中播放视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29071112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing Video in JavaFX8
提问by The Well
I am new to JavaFX and Im currently working with an application that plays a video(.MP4) at the beginning of the application, however, I cant seem to make it work! Please tell me what's wrong with my code:
我是 JavaFX 的新手,我目前正在使用一个在应用程序开头播放视频(.MP4)的应用程序,但是,我似乎无法让它工作!请告诉我我的代码有什么问题:
import java.io.File;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.text.Text;
public class NoConnection {
private StackPane root;
public NoConnection(){
Media media = null;
try{
File video = new File("video.mp4");
String url = video.toURI().toURL().toString();
System.out.println("URL: "+url);
media = new Media(url);
}catch(Exception e){
System.err.println(e.toString());
}
MediaPlayer player = new MediaPlayer(media);
player.play();
MediaView mediaView = new MediaView(player);
root = new StackPane();
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-background-color : white;");
root.getChildren().add(mediaView);
}
public StackPane getLayout(){
return root;
}
}
By the way, Im running Windows XP!
顺便说一下,我运行的是 Windows XP!
Here's the complete error:
这是完整的错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda/11461388.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda/31501478.run(Unknown Source)
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$null1(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda/29531133.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
回答by Roland
This is all there is to do: Create a java class with this code:
这就是所有要做的事情:使用以下代码创建一个 java 类:
public class VideoTest extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
root.getChildren().add( mediaView);
Scene scene = new Scene(root, 1024, 768);
primaryStage.setScene(scene);
primaryStage.show();
player.play();
}
}
and put the video.mp4 into the same folder. Start it. The video should play.
并将 video.mp4 放入同一个文件夹中。启动它。视频应该可以播放。
Btw I got a file not found exception using your code. And Windows XP isn't supported anymore. Neither by Oracle nor by Microsoft. If this simple code doesn't work, you got another problem.
顺便说一句,我使用您的代码遇到了未找到文件的异常。并且不再支持 Windows XP。无论是甲骨文还是微软。如果这个简单的代码不起作用,你就会遇到另一个问题。
回答by The Well
Here's the answer:
答案如下:
The FLV container is supported by the media stack on the platforms supported by the JavaFX SDK. A single movie encoded in this format works seamlessly on supported platforms. Standard FLV MIME settings are required on the server side to enable media streaming.
FLV 容器由 JavaFX SDK 支持的平台上的媒体堆栈支持。以这种格式编码的单个电影可在支持的平台上无缝运行。服务器端需要标准 FLV MIME 设置才能启用媒体流。
The MPEG-4 multimedia container is also supported on all operating systems supported by the JavaFX SDK. On the Mac OS X and Windows 7 platforms, playback will be functional without requiring additional software. However, the Linux operating system and versions of Windows older than Windows 7 require the installation of readily available third party software packages, as documented in the JavaFX System Requirements. AAC and H.264/AVC decoding have certain platform-dependent limitations, as described in the JavaFX Release Notes.
JavaFX SDK 支持的所有操作系统也支持 MPEG-4 多媒体容器。在 Mac OS X 和 Windows 7 平台上,无需额外软件即可进行播放。但是,Linux 操作系统和早于 Windows 7 的 Windows 版本需要安装随时可用的第三方软件包,如 JavaFX 系统要求中所述。AAC 和 H.264/AVC 解码具有某些平台相关的限制,如 JavaFX 发行说明中所述。
Decoding of some audio and video compression types relies on operating system-specific media engines. The JavaFX media framework does not attempt to handle all multimedia container formats and media encodings supported by these native engines. Instead, the framework attempts to provide equivalent and well-tested functionality across all platforms on which JavaFX is supported.
某些音频和视频压缩类型的解码依赖于操作系统特定的媒体引擎。JavaFX 媒体框架不会尝试处理这些本机引擎支持的所有多媒体容器格式和媒体编码。相反,该框架尝试在支持 JavaFX 的所有平台上提供等效且经过良好测试的功能。