I have a action listener on a media player using vlcj. When i run the program in debug mode the action listener triggers when the video is finished but when I run it normally in eclipse it does not trigger.
My action listener
public static void youtubeGui(){
Main.playing = true;
final JFrame f = new JFrame();
f.setLocation(100,100);
f.setSize(1000,600);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
Canvas c = new Canvas();
c.setBackground(Color.black);
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(c);
f.add(p);
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
MediaPlayerFactory mpf = new MediaPlayerFactory();
EmbeddedMediaPlayer emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(f));
emp.setVideoSurface(mpf.newVideoSurface(c));
emp.setPlaySubItems(true);
String str = Insert.videoQueue.peek();
emp.prepareMedia(str);
emp.play();
Main.playing = true;
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
emp.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void finished(MediaPlayer mediaPlayer) {
Insert.videoQueue.remove();
System.out.println("aaaaa");
f.setVisible(false);
f.dispose();
Main.playing = false;
}
});
}
Checking for new inserts method
public static void addCheck(String locationIn) throws IOException {
String fileLine = "";
String a = "";
while (true) {
Scanner inFile = new Scanner(new FileReader(
locationIn));
while (inFile.hasNext()) {
fileLine = inFile.nextLine();
}
if (fileLine.contains("watch?v=") && fileLine.contains("!add") && !fileLine.equals(a)) {
a = fileLine;
String result = fileLine.substring(fileLine.indexOf("[URL]") + 5, fileLine.indexOf("[/URL]"));
videoQueue.add(result);
result = "";
if(Main.playing == false){
Gui.youtubeGui();
}
}
inFile.close();
}
}
I'm guessing since I suspect your posted code does not tell the full story.
I have seen the garbage collector behave differently when running normal vs debug mode in Eclipse.
If you look in your youtubeGui
method you declare mpf
and emp
as local heap variables. When that method exits, there does not appear to be anything else keeping those two variables pinned so the referenced objects become eligible for garbage collection.
If your emp
object was indeed garbage collected, that might explain why you never see the listener being activated.
In debug mode, the garbage collection may have been deferred whereas in non-debug mode it runs sooner.
You might think that how can the media player have been garbage collected because the video is still playing? Well, the media player has created some native resources outside of the JVM and nothing has explicitly destroyed those native resources just because the Java media player object has been garbage collected. What would usually happen then some indeterminate time later is a native crash.
So I would suggest rearranging your code so that you have something keeping a hard reference to your media player (and maybe the media player factory too).