java - KeyListener 无法与 Java 中的 MediaPlayer 一起正常工作

标签 java media-player keylistener

我有一个关于与类 KeyListener (用于监听按键)和 MediaPlayer (用于播放介绍性视频)相关的 Java 问题。我的 KeyListener 独立工作良好(它能够读取正在按下的按键),但是当播放视频时,它无法读取任何按键,因此我可以得出结论,KeyListener 无法与 MediaPlayer 正常工作。

在我的应用程序中,当在介绍性视频期间按下“Escape”键时,它应该中断通过调用函数 thread.interrupt() 执行的线程,以便中止 Thread.sleep() 函数并停止视频。

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

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

  }

}

最佳答案

该问题的正确答案是:

我将 KeyListener 添加到变量 panelJavaFX (JFXPanel),现在在播放视频时按下“Escape”键时它可以读取。

关于java - KeyListener 无法与 Java 中的 MediaPlayer 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45033900/

相关文章:

android - 如何在android中播放音频文件

Java限制KeyListener更新

java - 处理 TableCellRenderer 上的整行

java - JPA 获取摘要对象的最佳方法是什么?

java - 我已经创建了一个带有 TextView 和 ImageView 的 Place 对象,但是当我尝试添加一个 place 对象时收到此错误

java - JTextField key listener 粘贴后落后一个

java - 通过 keyPressed 在 JLabel 中一个一个地更改值

java - Infinispan如何用于数据持久化?

ios - 将原始音乐文件从主包转换为 MPMediaItem

IOS MPMoviePlayerViewController 不断加载