简体   繁体   English

KeyListener无法与Java中的MediaPlayer一起正常使用

[英]KeyListener not working properly with MediaPlayer in Java

I have a question about Java related with the classes KeyListener (that is used for the listening of keys) and MediaPlayer (that is used for playing an introductory video). 我有一个关于Java的问题,它与类KeyListener (用于侦听键)和MediaPlayer (用于播放介绍性视频)有关。 My KeyListener is working good independently (it is able to read the key that is being pressed), but when the video is being played it can't read any key, so I can conclude that KeyListener is not working properly with MediaPlayer . 我的KeyListener独立运行良好(它能够读取被按下的键),但是在播放视频时它无法读取任何键,因此我可以得出结论, KeyListenerMediaPlayer不能正常工作。

In my application, when the "Escape" key is pressed during the introductory video it should make an interruption to a thread that has been executed by calling the function thread.interrupt() , so that it aborts the Thread.sleep() function and stops the video. 在我的应用程序中,在介绍性视频中按下“ Escape”键时,应该中断通过调用函数thread.interrupt()执行的线程,以便它中止Thread.sleep()函数,并且停止视频。

Here is the code of my application: 这是我的应用程序的代码:

/**
 * 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;

  // Define the variable for the key listener.
  public static KeyListener keyListener;

  // Define the variable for the Runnable interface.
  public static Runnable runnable;

  // Define the variable for a thread.
  public static Thread thread;

  /**
   * 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);

    // Enable the listening of keys.
    enableKeyListener();

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

    // Create the Runnable interface.
    runnable = new Runnable(){

      /**
       * Main function of the Runnable interface.
       */
      public void run(){

        try{

          // Show the introductory video.
          showVideo();

          // Pause the execution of the application for 30 seconds (duration of the introductory video).
          Thread.sleep(30000);

        }catch (InterruptedException interruptedException){

          // Stop the video if an interruption has been occurred.
          video.stop();

        }finally{

          // Set the background image.
          String filename = "./media/image/background.jpg";
          window.setContentPane(new JLabel(new ImageIcon(filename)));

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

        }

      }

    };

    // Create a new thread.
    thread = new Thread(runnable);

    // Start the execution of the thread.
    thread.start();

  }

  /**
   * Enables the listening of keys.
   */
  public static void enableKeyListener(){

    // Create the key listener.
    keyListener = new KeyListener(){

      // Set the behavior whenever a key is pressed.
      public void keyPressed(KeyEvent keyEvent){

        // Check if the "Escape" key is pressed.
        if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){

          // Check if the introductory video it is being played.
          if (video.getStatus().equals(Status.PLAYING)){

            // Make an interruption in the thread that is being executed.
            thread.interrupt();

          }

        }

      }

      // Set the behavior whenever a key is released.
      public void keyReleased(KeyEvent keyEvent){}

      // Set the behavior whenever a key is typed.
      public void keyTyped(KeyEvent keyEvent){}

    };

    // Add the key listener to the window of the game.
    window.addKeyListener(keyListener);

  }

  /**
   * 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();

  }

}

The correct answer to this question is: 这个问题的正确答案是:

I added the KeyListener to the variable panelJavaFX ( JFXPanel ) and now it can read when the "Escape" key is pressed while the video is being played. 我将KeyListener添加到变量panelJavaFXJFXPanel )中,现在可以在播放视频时按下“退出”键时读取它。

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

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