java视频播放

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.youkuaiyun.com/detail/qq_21549989/9700730


在运行这个程序的时候首先需要安装vlc这个播放器,

Native.loadLibrary("C:\\VideoLAN\\VLC\\libvlc.dll", LibVlc.class);

其中C:\\VideoLAN\\VLC\\libvlc.dll是安装路径下的一个dll,注意不能用\要用\\







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值