I'm trying to create a MediaPlayer with VLCJ and Swing, because with JavaFX I can't read .avi media. I have followed the tutorial on TutorialPoint, but the video is not showing and I can hear the sound.
This the code
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public 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 = "/Users/jul/Desktop/Harry Potter (2) et la chambre des secrets.avi";
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JButton playButton;
private JButton pauseButton;
public App(String title) {
super(title);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
}
public void initialize() {
this.setBounds(100, 100, 600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public 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);
contentPane.add(controlsPane, BorderLayout.SOUTH);
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().play();
}
});
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().pause();
}
});
this.setContentPane(contentPane);
this.setVisible(true);
}
public void loadVideo(String path) {
mediaPlayerComponent.mediaPlayer().media().startPaused(path);
}
public static void main( String[] args ){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.out.println(e);
}
App application = new App(TITLE);
application.initialize();
application.setVisible(true);
application.loadVideo(VIDEO_PATH);
}
}
You can see I have the window with the button, when I press the play and the pause button it's working, but the videos is not showing. I just have a black screen, with the sound.
Video is failing here because you are using EmbeddedMediaPlayerComponent
on macOS.
The EmbeddedMediaPlayerComponent
and by extension LibVLC, needs a heavyweight component like an AWT Canvas to render the video into. With Java on macOS there is no longer any heavyweight AWT, so it won't work.
In fact, I'm surprised there is not at least one error message apparent when you run your application.
The simplest solution here is to replace your EmbeddedMediaPlayerComponent
with a CallbackMediaPlayerComponent
. That is a literal one-line code change. The difference is the callback component renders (or "paints" in this context) each frame of video into whatever component you want.
This will work, but it will cost more CPU to render your video this way.
There are alternatives to this which are better in my opinion. One of those alternatives is to use vlcj with JavaFX, see here for an example: https://github.com/caprica/vlcj-javafx-demo.