我一直在尝试使用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解决了我的问题
答案 0 :(得分:0)
添加一个监听器:
mediaPlayer.statusProperty()。addListener(new ChangeListener(){ @覆盖 public void changed(ObservableValue observable,Object oldValue,Object newValue){ System.out.println(“mp.getStatus()=”+ mediaPlayer.getStatus()); System.out.println(“mediaPlayer.getTotalDuration()=”+ mediaPlayer.getTotalDuration()); } });