So I have read some old posts (from 2011) that Windows has an issue supporting mouse events while videos were playing and some Windows hooker had to be used. Then, I found a more recent post related to this issue here: https://github.com/caprica/vlcj/issues/78
At the end of this post, a method to receive mouse click events is discussed and the developer of VLCJ
himself endorsed it as the normal way to support the functionality:
Canvas videoSurface = this.mediaPlayerComponent.getVideoSurface();
videoSurface.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Click");
media_clicked();
}
});
Except, when I tried it, it did not work.
package test;
import com.sun.jna.NativeLibrary;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.JFrame;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class Test {
public static void main(String[] args) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = environment.getDefaultScreenDevice();
GraphicsConfiguration graphConfigure = device.getDefaultConfiguration();
JFrame frame = new JFrame(graphConfigure);
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setSize(dimension);
frame.setUndecorated(true);
frame.setVisible(true);
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
EmbeddedMediaPlayerComponent component = new EmbeddedMediaPlayerComponent();
EmbeddedMediaPlayer player = component.getMediaPlayer();
Canvas videoSurface = component.getVideoSurface();
videoSurface.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Click");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Click");
}
});
frame.getContentPane().add(component, BorderLayout.CENTER);
frame.repaint();
frame.setVisible(true);
String currDir = System.getProperty("user.dir");
player.playMedia(currDir + "\\sample_video_01.mp4");
}
}
So the video is able to play, but no where in the console the phrase "Clicked" gets printed even with clicking on the screen while the video is playing. I have also tried frame.getContentPane().add(videoSurface, BorderLayout.CENTER);
instead of frame.getContentPane().add(component, BorderLayout.CENTER);
, but neither is able to print "Clicked".
I am using NetBeans with jna-3.5.2
, platform-3.5.2
and vlcj-3.1.0
. Any pointer would be appreciated.
The vlcj project issue page you linked to refers to another issue, and that issue has the answer, including a fully working test.
Essentially you must do this (on Windows):
player.setEnableMouseInputHandling(false);
And maybe this:
player.setEnableKeyInputHandling(false);
What you're basically telling LibVLC here is "don't mess with the mouse (or keyboard) events" and send them to me instead.
This is not quite the whole story...
For keyboard events to be delivered you must manage focus correctly in your application. Clicking on the video surface does not (depending on your platform) automatically grab the focus.
One way to solve that is:
mediaPlayer.getVideoSurface().requestFocusInWindow();
This means when your window is focussed, e.g. by clicking, the focus will transfer to your video surface.
Here is a stable link to the test case:
None of this is necessary on Linux.