How can I play a video with vlcj in the background in JavaSwing?

56 Views Asked by At

I'm trying to play a video as a background with vclj, but it doesn't work, the screen appears black and now, I'm sharing my two files, which are Main and Home.

The project is in intelliJ, I leave a repository on github as well.

Github: https://github.com/flowerNkl/VideoBackgroundVLCJ.git

Main.java

package org.main;

import com.formdev.flatlaf.fonts.roboto.FlatRobotoFont;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
import com.formdev.flatlaf.util.UIScale;
import org.forms.Home;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main  extends JFrame {
    private Home home;
    public Main() {

        init();
    }

    private void init(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(false);
        setSize(UIScale.scale(new Dimension(1365, 768)));
        setLocationRelativeTo(null);
        home = new Home();
        setContentPane(home);

        addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                home.play();
            }

            @Override
            public void windowClosed(WindowEvent e) {
                home.stop();
            }
        });
    }

    public static void main(String[] args) {
        FlatRobotoFont.install();
        FlatMacDarkLaf.setup();
        UIManager.put("defaultFont", new Font(FlatRobotoFont.FAMILY, Font.PLAIN, 13));
        EventQueue.invokeLater(() -> new Main().setVisible(true));
    }

}

Home.java

package org.forms;

import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import java.awt.*;
import javax.swing.*;

public class Home extends JPanel {
    private MediaPlayerFactory factory;
    private EmbeddedMediaPlayer mediaPlayer;
    public Home() {
        init();
    }

    private void init(){
        factory = new MediaPlayerFactory();
        mediaPlayer = factory.mediaPlayers().newEmbeddedMediaPlayer();
        Canvas canvas = new Canvas();
        mediaPlayer.videoSurface().set(factory.videoSurfaces().newVideoSurface(canvas));

        setLayout(new BorderLayout());
        add(canvas);
    }

    public void play(){
        if(mediaPlayer.status().isPlaying()){
            mediaPlayer.controls().stop();
        }
        mediaPlayer.media().play("videos/WeatheringWithYou.mp4");
        mediaPlayer.controls().play();
    }

    public  void stop(){
        mediaPlayer.controls().stop();
        mediaPlayer.release();
        factory.release();
    }
}

I already tried to move the videos folder to the project resources folder and it still doesn't work, I also have VLC installed on the PC.

0

There are 0 best solutions below