繁体   English   中英

MediaPlayer在Java中无法正常工作

[英]MediaPlayer not working properly in Java

我有一个关于Java的问题,该类与用于播放介绍性视频的MediaPlayer类有关。 关键是,在运行我的应用程序时,有时会正确播放视频,有时可能无法正常播放视频,但大多数情况下却无法正常播放。 说它没有正确播放,是指音频已播放,但图像未播放。 因此,我可以得出结论, MediaPlayer无法正常工作。

这是我的应用程序的代码:

/**
 * Main class of the application.
 */
public class Main{

  // Define the variable for the window of the game.
  public static JFrame window;

  // Define the variable for the introductory video.
  public static MediaPlayer video;

  /**
   * Main function of the application.
   */
  public static void main(String[] args){

    // Prevent the JavaFX toolkit from closing.
    Platform.setImplicitExit(false);

    // Create the window of the game.
    window = new JFrame();

    // Set the title.
    window.setTitle("Chip");

    // Set the resolution as 1920 x 1280.
    window.setSize(1926,1343);

    // Set the location as in the middle of the screen.
    window.setLocationRelativeTo(null);

    // Set the operation when the window closes.
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Disable the maximization and resizable mode.
    window.setResizable(false);

    // Show the window.
    window.setVisible(true);

    // Show the introductory video.
    showVideo();

    // Pause the execution of the application for 30 seconds (duration of the introductory video).
    try{
      Thread.sleep(30000);
    }catch (InterruptedException interruptedException){
      interruptedException.printStackTrace();
    }

  }


  /**
   * Shows the introductory video.
   */
  public static void showVideo(){

    // Create the video panel and the JavaFX panel.
    JPanel panelVideo = new JPanel();
    JFXPanel panelJavaFX = new JFXPanel();

    // Set the size of the video panel as the resolution of the introductory video (1920 x 1080).
    panelVideo.setSize(1920,1080);

    // Set the location of the video panel as in the middle of the window of the game.
    int coordinateX = (window.getWidth() - panelVideo.getWidth() - window.getInsets().left - window.getInsets().right) / 2;
    int coordinateY = (window.getHeight() - panelVideo.getHeight() - window.getInsets().top - window.getInsets().bottom) / 2;
    panelVideo.setLocation(coordinateX,coordinateY);

    // Define the video file.
    String filename = "./media/video/introduction.mp4";
    video = new MediaPlayer(new Media(new File(filename).toURI().toString()));

    // Add the video to the JavaFX panel.
    panelJavaFX.setScene(new Scene(new Group(new MediaView(video))));

    // Add the JavaFX panel to the video panel.
    panelVideo.add(panelJavaFX);

    // Add the video panel to the window of the game.
    window.getContentPane().setLayout(null);
    window.add(panelVideo);

    // Play the video.
    video.play();

  }

}

设置您的video.play(); 在MediaPlayer初始化之后直接使用此方法,至少对于我来说似乎可以解决它,即:

video = new MediaPlayer(new Media(new File(filename).toURI().toString()));
video.play();

这个问题的正确答案是:

我直接将变量panelJavaFXJFXPanel )添加到变量windowJFrame )中,所以最终我没有使用中间变量panelVideoJPanel )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM