質問

I have created a simple VLCJ project that consists of a simple embedded player and a button to exit.

The code is as follows:

package test;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

public class Demo {


private final JFrame frame; 
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JPanel videoPane;
private JPanel buttonPane;
private Button exitButton;
private ActionListener a;

private static String vlc_location = "C:\\Program Files\\VideoLAN\\VLC";

public static void main(String[] args) {

    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlc_location);
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Demo().run();
        }
    });
}

public Demo() { 

    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

    a = new MyActionListener();
    exitButton = new Button("Exit");
    exitButton.setActionCommand("Exit app");        
    exitButton.addActionListener(a);

    buttonPane = new JPanel();
    buttonPane.setLayout(new BorderLayout());
    buttonPane.setBackground(Color.black);
    buttonPane.add(exitButton, BorderLayout.CENTER);

    videoPane = new JPanel();
    videoPane.setLayout(new BorderLayout());
    videoPane.setBackground(Color.black);
    videoPane.add(mediaPlayerComponent, BorderLayout.CENTER);
    videoPane.add(buttonPane, BorderLayout.PAGE_END);


    frame = new JFrame("vlcj demo");        
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(100, 100);
    frame.setSize(1200, 800);       
    frame.setContentPane(videoPane);        
    frame.setVisible(true);
}

public void run() {         
    mediaPlayerComponent.getMediaPlayer().playMedia(video_file);
}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String s = arg0.getActionCommand();

        if (s.equals("Exit")) {
            System.exit(0);
        }

    }

}

}

The problem is that the button does show up but it cannot be clicked. When i removed the videoPane, it was back to clickable! Any ideas if I'm missing something?

I am using the version 2.1.0 for vlcj.

Thanks!

役に立ちましたか?

解決

Thanks MadProgrammer for your advise. I went on to think about it and tried commenting away the line of code in run(). The JButton came back!

However, when i un-commented the code in run(), the JButton disappeared. I was thinking maybe the Swing runnable was causing issue with the creation of the JButton.

Hence, what i did was to comment away the whole Swing runnable and just use:

final Demo demo = new Demo();
demo.run();

The demo can now play video and display the Exit button, thanks!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top