本系统主要使用 SpringMVC+Hibernate+MySQL+VLC技术实现前端视频资源的播放。
播放器可以进行播放,暂停,进度条,选择播放文件,音量调节等。
前端页面主要实现用户的登录,注册,主页界面,电影界面,电视剧界面,动画片界面,播放记录界面,和管理员。
页面可以进行视频的下载,播放,删除(管理员才有权限)。
1.spring-hibernate.xml
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
#database connection config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/playvedio?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = ******
#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
public void playVedio(final String fileName) {
if (RuntimeUtil.isMac()) {
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib"
);
}else if (RuntimeUtil.isWindows()) {
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "D:\\playVedio\\VLC"
);
}else if (RuntimeUtil.isNix()) {
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "/home/linux/vlc/install/lib"
);
}
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
EventQueue.invokeLater(new Runnable() {
MainPlay frame ;
public void run() {
try {
frame = new MainPlay();
frame.setVisible(true);
String options[] = {"--subsdec-encoding=GB18030"};
VedioMessage vedioMessage =new VedioMessage();
vedioMessage.setPlayDate(new Date());
String vedioName=null;
if(fileName.contains("\\")){
int lastIndexOf = fileName.lastIndexOf("\\");
vedioName = fileName.substring( lastIndexOf+1,fileName.length());
}else if(fileName.contains("/")){
int lastIndexOf = fileName.lastIndexOf("/");
vedioName = fileName.substring( lastIndexOf+1,fileName.length());
}
vedioMessage.setVedioName(vedioName);
vedioMessage.setPosition(fileName.replace("\\", "/"));
vedioMessage.setType("电影");
//personRepository.save(vedioMessage);
frame.getMediaPlayer().prepareMedia(fileName,options);
new SwingWorker() {
@Override
protected String doInBackground() throws Exception {
while (true) {
long total = frame.getMediaPlayer().getLength();
long curr = frame.getMediaPlayer().getTime();
float percent = (float)curr/total;
publish((int)(percent*100));
Thread.sleep(100);
}
}
protected void process(java.util.List chunks) {
for (int v : chunks) {
frame.getProgressBar().setValue(v);
}
};
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class MainPlay extends JFrame{
/**
* created by [email protected]
*/
private static final long serialVersionUID = 3929395174555613196L;
private JPanel contentPane;
static 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 mnFile;
private JMenuItem mntmOpenVideo;
private JMenuItem mntmOpenSubtitle;
private JMenuItem mntmExit;
private JSlider slider;
/**
* Create the frame.
*/
public MainPlay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 616, 466);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmOpenVideo = new JMenuItem("Open Video...");
mnFile.add(mntmOpenVideo);
mntmOpenSubtitle = new JMenuItem("Open Subtitle...");
mnFile.add(mntmOpenSubtitle);
mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
mntmOpenVideo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openVideo();
}
});
mntmOpenSubtitle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openSubtitle();
}
});
mntmExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exit();
}
});
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel 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));
controlPanel = new JPanel();
panel.add(controlPanel);
btnStop = new JButton("Stop");
controlPanel.add(btnStop);
btnPlay = new JButton("Play");
controlPanel.add(btnPlay);
btnPause = new JButton("Pause");
controlPanel.add(btnPause);
slider = new JSlider();
slider.setValue(100);
slider.setMaximum(120);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
setVol(slider.getValue());
}
});
controlPanel.add(slider);
progress = new JProgressBar();
progress.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
jumpTo((float)x/progress.getWidth());
}
});
progress.setStringPainted(true);
panel.add(progress, BorderLayout.NORTH);
btnPause.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
pause();
}
});
btnPlay.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
play();
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
stop();
}
});
}
public static EmbeddedMediaPlayer getMediaPlayer() {
return playerComponent.getMediaPlayer();
}
public JProgressBar getProgressBar() {
return progress;
}
public static void play() {
getMediaPlayer().play();
}
public static void pause() {
getMediaPlayer().pause();
}
public static void stop() {
getMediaPlayer().stop();
}
public static void jumpTo(float to) {
getMediaPlayer().setTime((long)(to*getMediaPlayer().getLength()));
}
public static void setVol(int v) {
getMediaPlayer().setVolume(v);
}
public static void openVideo() {
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
getMediaPlayer().playMedia(file.getAbsolutePath());
}
}
public static void openSubtitle() {
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
getMediaPlayer().setSubTitleFile(file);
}
}
public static void exit() {
getMediaPlayer().release();
System.exit(0);
}
}
5.1、环境配置
(1)下载VLC VlC官网http://www.videolan.org/ 各个版本的下载地址http://download.videolan.org/pub/videolan/vlc/last/ 里面有32位和64位的区分,按照自己的系统下载对应的vlc版本,下载解压后提取;
5.2 . VLC maven dependency in pom. xml:
{code}