gpt4 book ai didi

javafx声音/视频播放

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:43 24 4
gpt4 key购买 nike

我一直在尝试使用 JavaFX 播放一些 mp3、视频,但没有结果。我还搜索了这个论坛上的每个主题,寻找解决方案,但没有成功......如果有人可以帮助我,我将不胜感激

这是我的代码:

  @Override
public void start(Stage primaryStage) throws MalformedURLException {
primaryStage.setTitle("Embedded Media Player");
Group root = new Group();
Scene scene = new Scene(root, 540, 241);
File f = new File("src/sample/test2.mp3");

Media media = new Media(f.toURI().toString());

MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);

MediaView mediaView = new MediaView(mediaPlayer);

System.out.println("mediaPlayer.getMedia() = " + mediaPlayer.getMedia().getSource());
System.out.println("mp.getStatus() = " + mediaPlayer.getStatus());
System.out.println("mediaPlayer.getTotalDuration() = " + mediaPlayer.getTotalDuration());
System.out.println("mediaPlayer.getMedia().getSource() = " + mediaPlayer.getMedia().getSource());

((Group)scene.getRoot()).getChildren().add(mediaView);
primaryStage.setScene(scene);
primaryStage.show();
mediaPlayer.play();

和输出:

mediaPlayer.getMedia() = file:/C:/Users/Piotr/Desktop/okon/src/sample/test2.mp3
mp.getStatus() = UNKNOWN
mediaPlayer.getTotalDuration() = UNKNOWN
mediaPlayer.getMedia().getSource() = file:/C:/Users/Piotr/Desktop/okon/src/sample/test2.mp3

编辑:迁移到 Ubuntu 解决了我的问题

最佳答案

添加监听器:

mediaPlayer.statusProperty().addListener(new ChangeListener() { @覆盖 公共(public)无效改变(ObservableValue observable,对象oldValue,对象newValue){ System.out.println("mp.getStatus() = "+ mediaPlayer.getStatus()); System.out.println("mediaPlayer.getTotalDuration() = "+ mediaPlayer.getTotalDuration()); } });

关于javafx声音/视频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com