简体   繁体   English

在vlcj中使用MouseListener

[英]Using MouseListener with vlcj

I'm currently developping a Java interface which needs to loop a HomeVideo until someone clicks on it to access the program. 我目前正在开发Java接口,该接口需要循环HomeVideo,直到有人单击它以访问程序为止。 I'm using vlcj to read the video and it works well. 我正在使用vlcj来阅读视频,并且效果很好。

However, I need to detect if someone is clicking the video. 但是,我需要检测是否有人在单击视频。 Sadly as mentionned in the wiki the media player needs to be placed in a heavyweight component that implies I MUST place it under a Canvas (which is an AWT object, not a Swing one). 可悲的是,正如Wiki中提到的那样,媒体播放器需要放置在重量级的组件中,这意味着我必须将其放置在Canvas(这是AWT对象,而不是Swing对象)下面。 Thus the solution here seems not be applicable to my problem. 因此, 这里的解决方案似乎不适用于我的问题。

Since then, I can't detect any click in the video (even though it works outside of the Canvas). 从那时起,我无法检测到视频中的任何点击(即使它在“画布”之外也可以)。

I know it is also possible to place the media player directly in the JFrame : 我知道也可以将媒体播放器直接放在JFrame中:

JFrame frame = new JFrame("My First Media Player");
frame.setBounds(100, 100, 600, 400);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
frame.setContentPane(mediaPlayerComponent);
frame.setVisible(true);
mediaPlayerComponent.getMediaPlayer().playMedia("./Video.mp4");

but that doesn't seem possible here : I'm using a CardLayout to navigate through my JPanel, including the HomePage containing the media player. 但这似乎不太可能:我正在使用CardLayout来浏览我的JPanel,包括包含媒体播放器的HomePage。

Here is a SSCCE with vlcj-3.10.1 , jna-4.1.0 , jna-platform-4.1.0 , slf4j-api-1.7.24 (and slf4j-simple-1.7.24 ) which executes vlcj in a Canvas contained in a JPanel with a MouseListener attached. 这是一个带SSCCE vlcj-3.10.1jna-4.1.0jna-platform-4.1.0slf4j-api-1.7.24 (和slf4j-simple-1.7.24 ),其包含在执行画布vlcj附加了MouseListener的JPanel。 When we click on the video, nothing happens but if we click outside (ie the Canvas), we get the coordinates. 当我们单击视频时,什么都没有发生,但是如果我们在外部(即“画布”)上单击,我们将获得坐标。

public class mediaplayer {

    JFrame frame;
    JPanel p;
    Canvas c;

    public static void main(final String[] args) {
        new NativeDiscovery().discover();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new mediaplayer(args);
            }
        });
    }    

    private mediaplayer(String[] args) {
        frame = new JFrame("vlcj player");
        frame.setSize(1200, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        p = new JPanel();
        p.setLayout(null); // Absolute positionning
        p.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                double x = me.getX();
                double y = me.getY();
                System.out.println("X and Y: " + x + " " + y);
            }
        });

        // heavyweight component where to place MediaPlayer
        c = new Canvas();
        c.setBackground(Color.black);
        c.setBounds(0, 0, 1000, 560);

        p.add(c);

        frame.add(p, BorderLayout.CENTER);

        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
        EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
        mediaPlayer.setRepeat(true);
        mediaPlayer.prepareMedia("./Video.mp4");
        mediaPlayer.play();
    }
}

Is there any way to use a MouseListener on a Canvas, or a way to use vlcj in a way that it allows to detect mouse clicks ? 有什么方法可以在Canvas上使用MouseListener,还是可以通过vlcj来检测鼠标单击的方法? Thanks by advance, 预先感谢,

What I'm asking here is a solution to counter the lack of connexion between AWT.Canvas and Swing by using something else (than a Canvas) or with a workaround. 我在这里要问的是一种通过使用其他方法(而不是Canvas)来解决AWT.Canvas和Swing之间缺少连接的解决方案。

With vlcj on Linux and Windows adding a MouseListener to the video surface Canvas should just work in the usual way. 在Linux和Windows上使用vlcj时,将MouseListener添加到视频界面中, Canvas应该可以以通常的方式工作。

If you use the vlcj MediaPlayerComponent encapsulation, this works (for me at least): 如果您使用vlcj MediaPlayerComponent封装,则此方法有效(至少对我而言):

mediaPlayerComponent.getVideoSurface().addMouseListener(listener);

If you don't use a MediaPlayerComponent , then just add your listener directly to your Canvas . 如果您不使用MediaPlayerComponent ,则只需将侦听器直接添加到Canvas

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

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