1.下载并安装与系统适配的VLC版本;
2.将如下以下文件添加到lib中,并添加到build path中;
注:采用jna-4的版本的时候,可能会出现找不到vlc的报错的兼容性问题,可采用换为低版本的jna解决;
3.创建两个类,一个为窗体,一个为主程序。
主程序:
package com.caihui.videoplayer.main;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
import com.caihui.videoplayer.views.MainWindow;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class PlayMain {//程序开启的方法
static MainWindow frame;
public static void main(String[] args) {
// TODO Auto-generated method stub
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");//VLC的外部库路径
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class);//加载
//创建窗体
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new MainWindow();
frame.setVisible(true);
String options[] = {"--subsdec-encoding=GB18030"};//声明中文编码
frame.getMediaPlayer().prepareMedia("video//2、工程创建与配置.mp4",options);//准备媒体文件
new SwingWorker() {
@Override
protected String doInBackground() throws Exception {
// TODO Auto-generated method stub
while(true) {
long totalTime = frame.getMediaPlayer().getLength();//获取视频长度,以毫秒记,长整型
long currentTime = frame.getMediaPlayer().getTime();//目前的进度
float percent = (float)currentTime/totalTime;
publish((int)(percent*100));
Thread.sleep(100);
}
};
protected void process(java.util.List chunks) {
for (int v : chunks) {
frame.getprograssBar().setValue(v);
}
};
}.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public static void play() {
frame.getMediaPlayer().play();
}
public static void pause() {
frame.getMediaPlayer().pause();
}
public static void stop() {
frame.getMediaPlayer().stop();;
}
public static void jumpto(float to) {
frame.getMediaPlayer().setTime((long)(to*frame.getMediaPlayer().getLength()));
}
public static void openVideo() {
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
java.io.File file =chooser.getSelectedFile();
frame.getMediaPlayer().playMedia(file.getAbsolutePath());
}
}
public static void opensubtitle() {
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
java.io.File file =chooser.getSelectedFile();
frame.getMediaPlayer().setSubTitleFile(file.getAbsolutePath());
}
}
public static void exit() {
frame.getMediaPlayer().release();
System.exit(0);
}
public static void setVol(int v) {
frame.getMediaPlayer().setVolume(v);
}
}
窗体:
package com.caihui.videoplayer.views;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.caihui.videoplayer.main.PlayMain;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JProgressBar;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
@SuppressWarnings("serial")
public class MainWindow extends JFrame {//播放器界面
private JPanel contentPane;
private JPanel videopane;
EmbeddedMediaPlayerComponent playerComponent;//播放器组件
private JPanel panel;
private JButton btnPlay;
private JButton btnPause;
private JButton btnStop;
private JPanel buttonPane;
private JProgressBar progress;
private JMenuBar menuBar;
private JMenu mnFile;
private JMenuItem mntmOpenVideo;
private JMenuItem mntmOpenSubtitle;
private JMenuItem mntmExit;
private JSlider slider;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setTitle("Tsai_Hui_VideoPlayer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 601, 422);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("file");
menuBar.add(mnFile);
mntmOpenVideo = new JMenuItem("open video");
mntmOpenVideo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PlayMain.openVideo();
}
});
mnFile.add(mntmOpenVideo);
mntmOpenSubtitle = new JMenuItem("open subtitle");
mntmOpenSubtitle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PlayMain.opensubtitle();
}
});
mnFile.add(mntmOpenSubtitle);
mntmExit = new JMenuItem("exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PlayMain.exit();
}
});
mnFile.add(mntmExit);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
videopane = new JPanel();
contentPane.add(videopane, BorderLayout.CENTER);
videopane.setLayout(new BorderLayout(0, 0));
playerComponent = new EmbeddedMediaPlayerComponent();
videopane.add(playerComponent);
panel = new JPanel();
videopane.add(panel, BorderLayout.SOUTH);
panel.setLayout(new BorderLayout(0, 0));
buttonPane = new JPanel();
panel.add(buttonPane);
btnStop = new JButton("Stop");
buttonPane.add(btnStop);
btnPlay = new JButton("Play");
buttonPane.add(btnPlay);
btnPause = new JButton("Pause");
buttonPane.add(btnPause);
slider = new JSlider();
slider.setValue(100);
slider.setMaximum(120);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
PlayMain.setVol(slider.getValue());
}
});
buttonPane.add(slider);
progress = new JProgressBar();
progress.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int currentX = e.getX();
// float to =(float)(currentX/progress.getWidth());
PlayMain.jumpto((float)currentX/progress.getWidth());
}
});
progress.setStringPainted(true);
panel.add(progress, BorderLayout.NORTH);
btnPause.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
PlayMain.pause();
}
});
btnPlay.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
PlayMain.play();
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
PlayMain.stop();
}
});
}
public EmbeddedMediaPlayer getMediaPlayer() {
return playerComponent.getMediaPlayer();
}
public JProgressBar getprograssBar() {
return progress;
}
}
运行画面:
主要实现功能:播放,暂停,停止,音量调节,进度调节,文件打开菜单栏等。