Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MediaPlayer在Java中工作不正常_Java_Media Player - Fatal编程技术网

MediaPlayer在Java中工作不正常

MediaPlayer在Java中工作不正常,java,media-player,Java,Media Player,我有一个关于Java的问题,与用于播放介绍性视频的类MediaPlayer有关。关键是,在运行我的应用程序时,视频有时播放正确,有时不正确,大多数情况下不正确。通过说它没有正确播放,我指的是音频播放,但图像没有。因此,我可以断定,MediaPlayer工作不正常 以下是我的申请代码: /** * Main class of the application. */ public class Main{ // Define the variable for the window of th

我有一个关于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()方法,至少对我来说,它似乎已修复,即:

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

这个问题的正确答案是:


我将变量
panelJavaFX
JFXPanel
)直接添加到变量
窗口(
JFrame
),因此我最终没有使用中间变量
panelVideo
JPanel
)。

您使用JavaFX吗?因为如果您是,我建议您使用MediaView而不是面板来处理MediaPlayers,我使用的是JavaFX。那么,代码会是什么样子呢?我不知道。对不起,我弄错了,你在用它。