[英]How to make video player in java using vlcj?
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class VLCPlayer {
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
//This is the path for libvlc.dll
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(() -> {
VLCPlayer vlcPlayer = new VLCPlayer();
});
}
private VLCPlayer() {
//MAXIMIZE TO SCREEN
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame();
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
frame.setContentPane(mediaPlayerComponent);
frame.setLocation(0, 0);
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayerComponent.getMediaPlayer().playMedia("D:\\centaur_1_xvid.avi");//Movie name which want to play
}
}
its running fine now as i'm using 3.0.1 version of vlcj and jna 3.5.2 i want to add speed(playbackrate) functionality in video player how can i do that and i want to know the timing when video paused and when played.它现在运行良好,因为我正在使用 3.0.1 版本的 vlcj 和 jna 3.5.2 我想在视频播放器中添加速度(播放率)功能我该怎么做,我想知道视频暂停和播放的时间.
Very simple.很简单的。 To get the time or time left, You can use $T
for time and $L
for time left.要获得剩余时间或剩余时间,您可以使用$T
表示时间,使用$L
表示剩余时间。
And in case of setting speed, You can simply use the api function namely在设置速度的情况下,您可以简单地使用api函数即
/**
* Set the video play rate.
* <p>
* Some media protocols are not able to change the rate.
*
* @param rate rate, where 1.0 is normal speed, 0.5 is half speed, 2.0 is double speed and so on
* @return -1 on error, 0 on success
*/
int setRate(float rate);
If You want to know more, check the documentation here如果您想了解更多信息,请查看此处的文档
UPDATE更新
To get the time , use this要获得时间,请使用此
/**
* Get the current play-back time.
*
* @return current time, expressed as a number of milliseconds
*/
long getTime();
check docs link above.检查上面的文档链接。
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class VLCPlayer {
private static JFileChooser filechooser = new JFileChooser();
//This is the path for libvlc.dll
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(() -> {
VLCPlayer vlcPlayer = new VLCPlayer();
});
}
private VLCPlayer() {
//MAXIMIZE TO SCREEN
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame();
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
Canvas c = new Canvas();
c.setBackground(Color.black);
JPanel p = new JPanel();
c.setBounds(100, 500, 1050, 500);
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
p.setBounds(100, 50, 1050, 600);
frame.add(p, BorderLayout.NORTH);
JPanel p1 = new JPanel();
p1.setBounds(100, 900, 105, 200);
frame.add(p1, BorderLayout.SOUTH);
JButton playbutton = new JButton("play");
//playbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/download.png"));
playbutton.setBounds(50, 50, 150, 100);
// playbutton.addActionListener((ActionListener) this);
p1.add(playbutton);
JButton pausebutton = new JButton("pause");
// pausebutton.setIcon(new ImageIcon("pics/pausebutton.png"));
pausebutton.setBounds(80, 50, 150, 100);
p1.add(pausebutton);
File ourfile;
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
filechooser.showSaveDialog(null);
ourfile = filechooser.getSelectedFile();
String mediaPath = ourfile.getAbsolutePath();
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
frame.setLocation(100, 100);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayer.playMedia(mediaPath);
pausebutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayer.pause();
// or mediaPlayer.pause() depending on what works.
final long time = mediaPlayer.getTime();
System.out.println(time);
}
});
playbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayer.play();
}
});
}
}
This is how i solved the problem Thanks.这就是我解决问题的方法 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.