package videoPlayer; import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; import com.sun.jna.Native; import uk.co.caprica.vlcj.binding.LibVlc; import uk.co.caprica.vlcj.component.EmbeddedMediaListPlayerComponent; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; public class videoPlayUI extends JFrame implements Runnable{ EmbeddedMediaListPlayerComponent playcomponent; public EmbeddedMediaPlayer mp; JProgressBar progress; VideoPlayListener my; String videoPlayPath; public videoPlayUI(String videoPlayPath) { Native.loadLibrary("C:\\VideoLAN\\VLC\\libvlc.dll", LibVlc.class); this.videoPlayPath = videoPlayPath; playcomponent = new EmbeddedMediaListPlayerComponent(); mp = playcomponent.getMediaPlayer(); my = new VideoPlayListener(mp,this,videoPlayPath); this.setSize(400, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); northPanel(); centerPanel(); this.setVisible(true); try { Thread.sleep(1000); mp.playMedia(videoPlayPath); new Thread(this).start(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void northPanel() { JPanel north = new JPanel(); north.setLayout(new BorderLayout()); north.add(northNorthPanel(),BorderLayout.NORTH); north.add(northCenterProcess()); this.add(north,BorderLayout.NORTH); } public JPanel northNorthPanel() { JPanel northnorth = new JPanel(); JButton start = new JButton("开始"); start.addActionListener(my); JButton pause = new JButton("暂停"); pause.addActionListener(my); JButton stop = new JButton("停止"); stop.addActionListener(my); northnorth.add(start); northnorth.add(pause); northnorth.add(stop); return northnorth; } public JProgressBar northCenterProcess() { progress = new JProgressBar(); progress.setStringPainted(true); progress.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int x = e.getX(); float percent = (float)x/progress.getWidth(); int value = x*100/progress.getWidth(); progress.setValue(value); mp.setTime((int)(percent*mp.getLength())); } }); return progress; } public void centerPanel() { this.add(playcomponent); } @Override public void run() { while(true){ float percent = (float)mp.getTime()/mp.getLength(); int temp = (int)(percent*100); System.out.println(temp); progress.setValue(temp); } } } package videoPlayer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import uk.co.caprica.vlcj.player.MediaPlayer; public class VideoPlayListener implements ActionListener { MediaPlayer mp; videoPlayUI ui; String videoPlayPath; public VideoPlayListener(MediaPlayer mp, videoPlayUI ui,String videoPlayPath) { this.mp = mp; this.ui = ui; this.videoPlayPath = videoPlayPath; } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("开始")){ mp.playMedia(videoPlayPath); try { Thread.sleep(500); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } new Thread(ui).start(); }else if (e.getActionCommand().equals("暂停")){ ((JButton)e.getSource()).setText("继续"); mp.pause(); }else if(e.getActionCommand().equals("停止")){ mp.stop(); }else if(e.getActionCommand().equals("继续")){ ((JButton)e.getSource()).setText("暂停"); mp.pause(); } } }
还需要几个jar包
下载地址 http://download.csdn.net/detail/qq_21549989/9700730
在运行这个程序的时候首先需要安装vlc这个播放器,
Native.loadLibrary("C:\\VideoLAN\\VLC\\libvlc.dll", LibVlc.class);
其中C:\\VideoLAN\\VLC\\libvlc.dll是安装路径下的一个dll,注意不能用\要用\\
