JavaFX视频无法播放

前端之家收集整理的这篇文章主要介绍了JavaFX视频无法播放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遵循了一些关于将 JavaFX与Swing(JFrame)结合起来播放视频的教程,但我得到的只是一个黑屏,其中视频应该没有任何实际内容播放,也没有报告任何错误.

我在这里做错了什么,为什么视频不播放?

我尝试了几个.flv视频,其中没有一个会开始播放(当我在浏览器中打开它们时它们会播放)

我在安装了K-lite完整编解码器包的Windows 8.1 N Pro上运行jre7和jdk1.7.0_45

编辑:更新我的代码后jewelsea的评论,没有任何改变,黑盒仍然出现没有内容播放,控制台没有显示任何文字出现

  1. package com.example.test;
  2.  
  3. import javafx.application.Platform;
  4. import javafx.embed.swing.JFXPanel;
  5. import javafx.event.Event;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Group;
  8. import javafx.scene.Scene;
  9. import javafx.scene.SceneBuilder;
  10. import javafx.scene.media.Media;
  11. import javafx.scene.media.MediaErrorEvent;
  12. import javafx.scene.media.MediaPlayer;
  13. import javafx.scene.media.MediaView;
  14. import javafx.scene.paint.Color;
  15.  
  16. import javax.swing.*;
  17.  
  18. public class Main {
  19. public static void main(String[] args) {
  20. SwingUtilities.invokeLater(new Runnable() {
  21. @Override
  22. public void run() {
  23. initAndShowGUI();
  24. }
  25. });
  26. }
  27.  
  28. private static void initAndShowGUI() {
  29. // This method is invoked on the EDT thread
  30. JFrame frame = new JFrame("Test");
  31. final JFXPanel fxPanel = new JFXPanel();
  32. frame.add(fxPanel);
  33. frame.setSize(640,480);
  34. frame.setVisible(true);
  35. frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  36.  
  37. Platform.runLater(new Runnable() {
  38. @Override
  39. public void run() {
  40. initFX(fxPanel);
  41. }
  42. });
  43. }
  44.  
  45. private static void initFX(JFXPanel fxPanel) {
  46. // This method is invoked on the JavaFX thread
  47. Scene scene = createScene();
  48. fxPanel.setScene(scene);
  49. }
  50.  
  51. private static Scene createScene() {
  52. String source;
  53. Media media;
  54. MediaPlayer mediaPlayer;
  55. MediaView mediaView = null;
  56. try {
  57. media = new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
  58. if (media.getError() == null) {
  59. media.setOnError(new Runnable() {
  60. public void run() {
  61. // Handle asynchronous error in Media object.
  62. System.out.println("Handle asynchronous error in Media object");
  63. }
  64. });
  65. try {
  66. mediaPlayer = new MediaPlayer(media);
  67. mediaPlayer.setAutoPlay(true);
  68.  
  69. if (mediaPlayer.getError() == null) {
  70. mediaPlayer.setOnError(new Runnable() {
  71. public void run() {
  72. // Handle asynchronous error in MediaPlayer object.
  73. System.out.println("Handle asynchronous error in MediaPlayer object");
  74. }
  75. });
  76. mediaView = new MediaView(mediaPlayer);
  77. mediaView.setOnError(new EventHandler() {
  78. public void handle(MediaErrorEvent t) {
  79. // Handle asynchronous error in MediaView.
  80. System.out.println("Handle asynchronous error in MediaView: "+ t.getMediaError());
  81. }
  82.  
  83. @Override
  84. public void handle(Event arg0) {
  85. // TODO Auto-generated method stub
  86. System.out.println("Handle asynchronous error in MediaView arg0: "+arg0.toString());
  87. }
  88. });
  89. } else {
  90. // Handle synchronous error creating MediaPlayer.
  91. System.out.println("Handle synchronous error creating MediaPlayer");
  92. }
  93. } catch (Exception mediaPlayerException) {
  94. // Handle exception in MediaPlayer constructor.
  95. System.out.println("Handle exception in MediaPlayer constructor: "+ mediaPlayerException.getMessage());
  96. }
  97. } else {
  98. // Handle synchronous error creating Media.
  99. System.out.println("Handle synchronous error creating Media");
  100. }
  101. } catch (Exception mediaException) {
  102. // Handle exception in Media constructor.
  103. System.out.println("Handle exception in Media constructor: "+mediaException.getMessage());
  104. }
  105.  
  106. Group root = new Group();
  107. Scene scene = SceneBuilder.create().width(640).height(480).root(root).fill(Color.WHITE).build();
  108.  
  109. if(mediaView != null) {
  110. root.getChildren().add(mediaView);
  111. }
  112.  
  113. return scene;
  114. }
  115. }

解决方法

所以我安装了windows media功能包以便adobe premiere pro正常工作(因为它需要一个来自windows media player的dll文件(我没有安装,因为我运行N版本的Windows)现在视频确实播放了为了我.

我不能说100%确认原因是没有安装WMP,因为媒体功能包可能已经安装了其他解决了我的问题的东西,但问题解决了:)

我要感谢其他尝试的答案,我真的很感激.

猜你在找的Java相关文章