簡體   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