java JavaFX - 使用媒体播放器和媒体视图播放视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509331/
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
JavaFX - playing video with mediaplayer and mediaview
提问by Libor Zapletal
This is what I have in controller:
这是我在控制器中的内容:
@FXML
private MediaView mediaView;
In method that handle button pressed I got filechooser:
在按下手柄按钮的方法中,我得到了文件选择器:
File file = fileChooser.showOpenDialog(null);
if(file != null){
initPlayer(file.toURI().toString());
}
and this is method initPlayer:
这是方法initPlayer:
private void initPlayer (String uri) {
if (uri == null)
return;
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer = null;
}
Media media = new Media(uri);
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaView = new MediaView(mediaPlayer);
mediaPlayer.setOnReady(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
}
this is part of code from my view created in scene builder:
这是我在场景构建器中创建的视图的一部分代码:
<Pane layoutX="80.0" layoutY="14.0" prefHeight="480.0" prefWidth="640.0">
<MediaView fx:id="mediaView" fitHeight="480.0" fitWidth="640.0" />
</Pane>
When I choose file I am hearing sound but I don′t see video. What′s problem with this code? What I am missing?
当我选择文件时,我听到声音但看不到视频。这段代码有什么问题?我缺少什么?
回答by jewelsea
A MediaView
instance is created by the FXMLLoader
and placed in your Pane
when you load your FXML file.
一个MediaView
实例是创建FXMLLoader
和放置在你Pane
当你加载FXML文件。
You don't need to create a new MediaView
and when you do it is not being attached to the scene which is why you can't see anything.
你不需要创建一个新的MediaView
,当你这样做时它不会被附加到场景中,这就是你看不到任何东西的原因。
Instead of:
代替:
mediaView = new MediaView(mediaPlayer);
Write:
写:
mediaView.setMediaPlayer(mediaPlayer);
回答by snoblucha
If you hear only the sound and you are on Windows XP computer there may be missing codecs.
如果您只听到声音并且您使用的是 Windows XP 计算机,则可能缺少编解码器。
See http://docs.oracle.com/javafx/2/system_requirements_2-2/jfxpub-system_requirements_2-2.htm
请参阅http://docs.oracle.com/javafx/2/system_requirements_2-2/jfxpub-system_requirements_2-2.htm
Or you can check the error player provides, if there's any
或者您可以检查播放器提供的错误,如果有的话
mediaPlayer.setOnError(new Runnable() {
@Override
public void run() {
String message = mediaPlayer.errorProperty().get().getMessage();
System.out.println(message);
}
});
回答by diego matos - keke
In your Fxml you need
在你的 Fxml 你需要
<FlowPane alignment="CENTER" columnHalignment="CENTER" layoutX="9.0" layoutY="48.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="9.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="48.0">
<children>
<MediaView fx:id="mediaView">
</MediaView>
</children>
</FlowPane>
and in you controller :
在你的控制器中:
@FXML private MediaPlayer mediaPlayer;
@FXML private Duration duration;
@FXML private MediaView mediaView;
....
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
init();
} catch (MalformedURLException ex) {
Logger.getLogger(MainOnlineController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void init() throws MalformedURLException {
....
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.setOnPlaying(new Runnable() {
public void run() {
if (stopRequested) {
mediaPlayer.pause();
stopRequested = false;
} else {
playButton.setText("||");
}
}
});
...other methods for mediaPlayer