java视频播放
2016-12-03 15:02
281 查看
package videoPlayer; public class Main { public static void main(String[] args) { String path = "D:\\2.avi"; videoPlayUI ui = new videoPlayUI(path); } }
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,注意不能用\要用\\
相关文章推荐
- java播放视频
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- java获取视频播放时长
- java读取视频播放时间,很有用啊
- java 播放视频
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- java调用播放器并播放视频
- java平台利用jsoup开发包,抓取优酷视频播放地址与图片地址等信息。
- java jmf视频播放器无法播放视频问题
- Java简单实现视频录制播放功能
- java播放视频
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- java调用播放器并播放视频
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- Java - 抓取优酷网视频播放页面(使用jsoup解析html,正则表达式处理字符串)
- java制作仿微信视频播放控件
- Android(Java):视频播放升级版——播放m3u8
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- 用java写简单的web服务器,在宿舍提供视频播放