您的位置:首页 > 编程语言 > Java开发

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 图形 swing class