
利用VLC自制一个java视频播放器
1.2 下载控制vlc播放器的jar包(vlcj)2.1开启Eclipse创建项目MediaPlayerForJava,在根目录下创建连个文件夹 lib,vlc.lib文件夹用来放vlcj下的jar包,vlc文件夹用来放vlc播放器的插件。

0.png
2.2将下载出来的vlcj解压,选中这四个文件java本地视频播放器,复制粘贴到 项目中的lib文件夹下,并创建模式(选中 lib下的四个jar包,右击 - BuildPath-AddToBuildPath)


1.PNG
2.3 如果下载的vlc播放器是zip版的java本地视频播放器,解压后复制根目录下的
文件libvlc.dll,文件libvlccore.dll,文件夹piugins,到项目中的vlc文件夹。如果是exe版的,找到安装目录,同样找到这两个文件和一个文件夹复制粘贴到vlc中。

1.png
2.4在src中创建一个包com.feihuang.main,创建一个类Main,创建一个包com.feihuang.view,创建一个类View.写下面代码,运行一下。

Main.java
package com.feihuang.main;
import javax.swing.SwingUtilities;
import com.feihuang.view.View;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
frame.setTitle("MediaPlayer");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
View.java
package com.feihuang.view;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class View extends JFrame {
private JPanel contentPane;
public View() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}

e.png

NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),
"vlc");
即之前我们将播放器组件放到了vlc文件夹下,让程序到vlc文件夹下去找
//创建一个容器放播放器组件对象
JPanel player = new JPanel();
contentPane.add(player, BorderLayout.CENTER);
player.setLayout(new BorderLayout(0, 0));
//创建播放器组件并添加到容器中去
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
player.add(mediaPlayerComponent);
public EmbeddedMediaPlayer getMediaPlayer(){
return mediaPlayerComponent.getMediaPlayer();
}
Main.java
package com.feihuang.main;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.feihuang.view.View;
import com.sun.jna.NativeLibrary;
public class Main {
private static View frame;
public static void main(String[] args) {
//new NativeDiscovery().discover();
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),
"vlc");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
frame = new View();
frame.setTitle("MediaPlayer");
frame.setVisible(true);
frame.getMediaPlayer().playMedia("c:\\mysourse\\1.mp4");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

View.java
package com.feihuang.view;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
public class View extends JFrame {
private JPanel contentPane;
private EmbeddedMediaPlayerComponent mediaPlayerComponent;
public View() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel player = new JPanel();
contentPane.add(player, BorderLayout.CENTER);
player.setLayout(new BorderLayout(0, 0));
//创建播放器组件并添加到容器中去
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
player.add(mediaPlayerComponent);
}
public EmbeddedMediaPlayer getMediaPlayer(){
return mediaPlayerComponent.getMediaPlayer();
}
}

P.png

Paste_Image.png

Paste_Image.png
问题就解决了~
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-128920-1.html
阿利伯克导弹驱逐舰