当前位置: 首页 > 工具软件 > vlcj > 使用案例 >

使用java制作一个自己的视频播放器VLCJ

闻鹤龄
2023-12-01

需求

想实现一个可以根据指令无限循环的视频播放让软件,简单的狠就是单纯的播放视频,不需要其他功能什么快进呀,暂停美化呀什么的!

开始

maven

<dependencies>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>uk.co.caprica</groupId>
            <artifactId>vlcj</artifactId>
            <version>3.10.1</version>
        </dependency>
    </dependencies>

接下来就是代码的实现了
MyWindow

package com.neon.vidoe;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MyWindow extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    EmbeddedMediaPlayerComponent playerComponent;
    private JPanel panel;
    private JButton btnPlay;
    private JButton btnPause;
    private JButton btnStop;
    private JPanel controlpanel;
    private JProgressBar progress;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem openmenu;
    private JMenuItem closemenu;
    private JSlider slider;

    public MyWindow() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension dimension = kit.getScreenSize();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setBounds(0, 0, dimension.width, dimension.height);
        setUndecorated(true);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(0, 0,0, 0));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JPanel Videopanel = new JPanel();
        contentPane.add(Videopanel, BorderLayout.CENTER);
        Videopanel.setLayout(new BorderLayout(0, 0));
        playerComponent = new EmbeddedMediaPlayerComponent();
        Videopanel.add(playerComponent);
        panel = new JPanel();
        Videopanel.add(panel, BorderLayout.SOUTH);
        panel.setLayout(new BorderLayout(0, 0));
    }

    public EmbeddedMediaPlayer  getMediaPlayer() {

        return playerComponent.getMediaPlayer();
    }
    public JProgressBar getProgressBar(){

        return progress;
    }

}

PlayerMain

package com.neon.vidoe;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;

import javax.swing.*;

public class PlayerMain {
    static MyWindow frame;
    private static boolean flag=true;
    public static void main(String[] args) {
        boolean found = new NativeDiscovery().discover();
        System.out.println("VLC 本地库调用是否成功 ---->>>>> " + found);
        System.out.println("VLC 本地库  版本  ---->>>>> " + LibVlc.INSTANCE.libvlc_get_version());
        frame = new MyWindow();
        frame.setVisible(true);

        openVideo("C:\\帅就完事了.mp4");
        new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                while(flag){
                    //实现循环播放
                    long total = frame.getMediaPlayer().getLength();
                    if (total==-1){
                        play();
                    }
                }
                return null;
            }
        }.execute();

    }



    //打开文件
    public static void openVideo(String path) {

        frame.getMediaPlayer().playMedia(path);

    }
    public static void play() {
        frame.getMediaPlayer().play();
    }

    public static void close(){
        flag=false;
        frame.getMediaPlayer().stop();
        frame.dispose();
    }

}

然后就实现了我们想要的效果!

代码地址

地址

 类似资料: