Reputation: 3
I am using vlcj on my java program. I need the video to play repeatedly. I've used setRepeat(true); but it doesn't work for me. Is this any other to loop the video? Or am I doing it wrong? Please help me. Thanks a lot.
public QueueMonitor() {
initComponents();
//VIDEO
chargerLibrairie();
Canvas c = new Canvas();
panel.add(c);
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
//Create a media player instance
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
mediaPlayer.playMedia("Ospital1.mp4");
mediaPlayer.setRepeat(true);
mediaPlayer.setPlaySubItems(true);
}
public static void chargerLibrairie(){
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
LibXUtil.initialise();
}
Also, how can I put some functions like pause, play, lower volume while the program is running? Like how a normal video player is able to do.
Upvotes: 0
Views: 740
Reputation: 1
public class Video extends javax.swing.JPanel
{
private EmbeddedMediaPlayerComponent video;
private EmbeddedMediaPlayer mediaPlayer;
public Video()
{
initComponents();
initVideo();
}
private void initVideo()
{
video = new EmbeddedMediaPlayerComponent()
{
@Override
public void finished(MediaPlayer mediaPlayer)
{
super.finished(mediaPlayer);
System.out.println("FINISHED");
new Thread(() -> {
System.gc();
mediaPlayer.controls().setTime(0);
mediaPlayer.controls().play();
}).start();
}
};
mediaPlayer = video.mediaPlayer();
mediaPlayer.input().enableKeyInputHandling(false);
mediaPlayer.input().enableMouseInputHandling(false);
add(video, BorderLayout.CENTER);
}
public void start(String path)
{
mediaPlayer.media().prepare(path);
mediaPlayer.controls().play();
}
public void stop()
{
mediaPlayer.controls().stop();
mediaPlayer.release();
video.release();
}
@SuppressWarnings("unchecked")
private void initComponents()
{
setLayout(new java.awt.BorderLayout());
}
}
Upvotes: 0
Reputation: 4146
Here are some minimal examples of how to get repeat play working with vlcj.
For current versions of vlcj, 4.x and later:
// vlcj 4.x+
public class RepeatPlayer {
public static void main(String[] args) throws Exception {
String mrl = "some-cool-video.mp3";
EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
JFrame f = new JFrame("Repeat Player");
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
mediaPlayerComponent.release();
}
});
f.setContentPane(mediaPlayerComponent);
f.setVisible(true);
mediaPlayerComponent.mediaPlayer().controls().setRepeat(true);
mediaPlayerComponent.mediaPlayer().media().play(mrl);
Thread.currentThread().join();
}
}
Since it looks like you're using an ancient version of vlcj, this is how it was done previously:
// vlcj 3.12.1
public class RepeatPlayer {
public static void main(String[] args) throws Exception {
String mrl = "some-cool-video.mp3";
final EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
JFrame f = new JFrame("Repeat Player");
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
mediaPlayerComponent.release();
}
});
f.setContentPane(mediaPlayerComponent);
f.setVisible(true);
mediaPlayerComponent.getMediaPlayer().setRepeat(true);
mediaPlayerComponent.getMediaPlayer().playMedia(mrl);
Thread.currentThread().join();
}
}
The code in the original question was therefore broadly correct at least with regards to the repeat-play functionality. The most likely problem with the original code is that the media player was garbage collected due to a failure to hold the object references.
Upvotes: 1