如何在javafx中从FileChooser播放视频 [英] how I can play an video from FileChooser in javafx
本文介绍了如何在javafx中从FileChooser播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
希望您能帮助我解决此问题,我尝试在所有程序中通过类似选项文件的 Button
Open 来使此代码用于播放视频.不行我不知道怎么了.
I hope you to help me to resolve this problem , I try make this code for play video from an Button
Open like option file in all the program but it doesn't work. I don't know what's wrong.
这是我的代码:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser chooser = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("select your media(*.mp4)", "*.mp4");
chooser.getExtensionFilters().add(filter);
File file = chooser.showOpenDialog(primaryStage);
if ( file !=null){
Media source = new Media(file.getPath());
MediaPlayer player = new MediaPlayer(source);
MediaView view = new MediaView(player);
root.getChildren().add(view);
player.play();
}
else {
label.setText("vide ");
}
推荐答案
您将路径传递到 toURI
:
You pass the path to the constructor of Media
. This constructor expects a URI string however. Fortunately File
can be converted to URI
using toURI
:
Media source = new Media(file.toURI().toString());
这篇关于如何在javafx中从FileChooser播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文