Vlcj安装及在SWT中调用实现播放器(参考网上的资料自己总结的,希望对大家有用)
1.安装VLC播放器
(win7 -64位)
下载VLC网址 :http://www.videolan.org/
下载vlcj:http://capricasoftware.co.uk/#/projects/vlcj
2.所需jar包:
3.环境配置:
在VLC的安装目录:
将这三个文件拷贝到Java工程目录下。如
4.代码导入VLC
5.SWT实现播放视频源码
package temp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaListPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.runtime.x.LibXUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.Frame;
import org.eclipse.swt.awt.SWT_AWT;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JRootPane;
public class playMVtest extends Shell {
private Composite composite_play;
public static JRootPane rootPane; // 放音乐的控件
public static boolean isplay;
static EmbeddedMediaListPlayerComponent mediaplay; // 播放视频的对象
/**
* Launch the application.
*
* @param args
*/
private Label lbl_add;
private Label label;
private Button btn_play;
private Button button;
public static void main(String args[]) {
try {
Display display = Display.getDefault();
playMVtest shell = new playMVtest(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* @param display
*/
public playMVtest(Display display) {
super(display, SWT.NONE);
// 引入vlc播放器
String ruta = "D:/software/VLC";
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), ruta);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
LibXUtil.initialise();
String filepath = "D://video//还来得及 - 黄子韬.mp4"; // 要播放的视频
File file = new File(filepath);
setBackground(new Color(null, 51, 51, 51));
setBackgroundMode(SWT.INHERIT_FORCE);
setSize(870, 540);
Composite comfoot = new Composite(this, SWT.NONE);
comfoot.setBounds(0, 480, 870, 60);
comfoot.setBackground(new Color(null, 36, 36, 36));
comfoot.setBackgroundMode(SWT.INHERIT_FORCE);
label = new Label(comfoot, SWT.NONE);
label.setFont(SWTResourceManager.getFont("楷体", 12, SWT.NORMAL));
label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
label.setBounds(10, 10, 367, 23);
// 解析播放的视频,显示在页面上
int indexi = filepath.lastIndexOf("/");
int index1 = filepath.lastIndexOf("-");
int index2 = filepath.lastIndexOf(".");
label.setText(filepath.substring(indexi + 1, index1) + " - " + filepath.substring(index1 + 2, index2));
Composite com01 = new Composite(this, SWT.NONE);
com01.setBounds(0, 433, 870, 48);
com01.setBackground(new Color(null, 36, 36, 36));
com01.setBackgroundMode(SWT.INHERIT_FORCE);
Label label_1 = new Label(com01, SWT.SEPARATOR | SWT.HORIZONTAL);
label_1.setBounds(0, 46, 871, 2);
label_1.setBackground(new Color(null, 50, 50, 50));
lbl_add = new Label(com01, SWT.NONE);
lbl_add.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
lbl_add.setFont(SWTResourceManager.getFont("楷体", 12, SWT.NORMAL));
lbl_add.setBounds(765, 15, 95, 17);
lbl_add.setText("本地导入>>>");
btn_play = new Button(com01, SWT.NONE);
btn_play.setBounds(10, 13, 80, 27);
btn_play.setText("暂停");
// 播放视频的容器SWT_AWT的Composite
composite_play = new Composite(this, SWT.EMBEDDED);
composite_play.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
composite_play.setBounds(0, 46, 870, 381);
Frame frame = SWT_AWT.new_Frame(composite_play);
Panel panel = new Panel();
frame.add(panel);
panel.setLayout(new BorderLayout(0, 0));
rootPane = new JRootPane();
panel.add(rootPane);
// 初始化播放器对象
mediaplay = new EmbeddedMediaListPlayerComponent();
rootPane.setContentPane(mediaplay); // 将播放对象放入播放容器中
// 关闭按钮
button = new Button(this, SWT.NONE);
button.setBounds(778, 10, 80, 27);
button.setText("关闭");
mediaplay.getMediaPlayer().playMedia(file.getAbsolutePath());
doEvent(); // 事件 的处理
}
private void doEvent() {
// 点击关闭程序
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.exit(0);
}
});
// 添加本地视频播放
lbl_add.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
// 导入本地视频播放前先将正在播放的视频停止
mediaplay.getMediaPlayer().stop();
btn_play.setText("暂停");
FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
fd.setText("选择MV");
fd.setFilterExtensions(new String[] { "*.mp4" });
String str = fd.open();
if (str != null) {
mediaplay.getMediaPlayer().playMedia(str);
btn_play.setText("播放");
// 解析播放的视频,显示在页面上
int indexi = str.lastIndexOf("\\");
int index1 = str.lastIndexOf("-");
int index2 = str.lastIndexOf(".");
label.setText(str.substring(indexi + 1, index1) + "- " + str.substring(index1 + 2, index2));
}
}
});
// 点击播放暂停
btn_play.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent arg0) {
if (isplay == false) {
btn_play.setText("播放");
mediaplay.getMediaPlayer().play();
isplay = true;
} else if (isplay == true) {
btn_play.setText("暂停");
mediaplay.getMediaPlayer().pause();
isplay = false;
}
}
});
}
@Override
protected void checkSubclass() {
}
}