-2

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();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Peter Maher
  • 99
  • 1
  • 10
  • 1
    I'm guessing that you have a GUI threading issue, that you're GUI is being blocked due to long-running code that is running on the GUI's event thread, but I'll be darned if I can prove it as I don't fully understand your code. Please consider creating and posting a valid [mcve]. – Hovercraft Full Of Eels Feb 21 '17 at 01:27
  • Also, if you format your code better so that it follows Java standards, it would be easier for all of us to understand and read. – Hovercraft Full Of Eels Feb 21 '17 at 01:27
  • I've formatted your code for readability, but in the future I strongly recommend that you do this yourself, especially by giving your code proper indentations, usually 4 spaces per block, and making sure that all code on the same block is on the same indentation level. Formatting is very important because if your code is not in a standard accepted format, it's not very readable, and if it's not readable, it's difficult to debug and to understand. – Hovercraft Full Of Eels Feb 21 '17 at 01:31
  • 1
    the downvotes seem quite unfair, i've seen far worse questions than this – caprica Feb 21 '17 at 23:38

1 Answers1

0

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).

caprica
  • 3,902
  • 4
  • 19
  • 39