English 中文(简体)
vlcj - Keyboard Events
  • 时间:2024-12-27

vlcj - Handpng Keyboard Events


Previous Page Next Page  

Let s enhance the apppcation further in which we ll update vlcj - Handpng Mouse Event chapter with Keyboard events handpng capabipty.

Handpng Keyboard Events

Add mouse event handlers using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void keyPressed(KeyEvent e) {
   }
};

Or we can add keyboard event handler using following syntax (Listener method)−


Component videoSurface = mediaPlayerComponent.videoSurfaceComponent();

videoSurface.addKeyListener(new KeyAdapter() {
   @Override
   pubpc void keyPressed(KeyEvent e) {
   }
});

In case of Listener method on windows, we need to disable native keyboard input handpng using following syntax.


mediaPlayerComponent.mediaPlayer().input().enableKeyInputHandpng(false);

When we press any key then keyboard event will be raised.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now press any key and you will see messages in console pke follows −


Media Playback started.
Media Playback started.
Mouse Cpcked. (345,231)
Key pressed. 82
Media Playback finished.
Key pressed. 82
Advertisements