Correct using VLCj

dzav picture dzav · Oct 4, 2012 · Viewed 8.3k times · Source

I try to use VLCj to get access to web-cameras. I am using this code:

public static void main(String[] args) {
    // Create player.
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    DirectMediaPlayer mediaPlayer = mediaPlayerFactory.newDirectMediaPlayer(
            320, 240, 
            new RenderCallback() {
                @Override
                public void display(Memory arg0) {
                    // Do nothing.
                }
         });

    // Options setup.
    String[] options = new String[]{};
    String mrl = "v4l2:///dev/video0"; // Linux

    // Start preocessing.
    mediaPlayer.startMedia(mrl, options);

    BufferedImage bufImg;
    for (int i = 0; i < 1000; ++i) {
        bufImg = mediaPlayer.getSnapshot();

        // Do something with BufferedImage...
        // ...
    }

    // Stop precessing.
    mediaPlayer.stop();
    mediaPlayer = null;

    System.out.println("Finish!");
}

And this code partially works -- I can get and work with BufferedImage, but:

  • I got an error in to output: [0x7f0a4c001268] main vout display error: Failed to set on top
  • When main loop is finished and camera was disabled program don't finished! I see Finish! message, but program not return control into IDE or console.

UPD: I am using openSUSE 12.2 x64, VLC 2.0.3 installed and working properly for all video files, library VLCj 2.1.0.

Answer

dzav picture dzav · Oct 6, 2012

This code working properly:

public static void main(String[] args) {
    // Configure player factory.
    String[] VLC_ARGS = {
            "--intf", "dummy",          // no interface
            "--vout", "dummy",          // we don't want video (output)
            "--no-audio",               // we don't want audio (decoding)
            "--no-video-title-show",    // nor the filename displayed
            "--no-stats",               // no stats
            "--no-sub-autodetect-file", // we don't want subtitles
            "--no-inhibit",             // we don't want interfaces
            "--no-disable-screensaver", // we don't want interfaces
            "--no-snapshot-preview",    // no blending in dummy vout
    };
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(VLC_ARGS);

    // Create player.
    HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();

    // Select input device.
    String mrl = "v4l2:///dev/video0";  // Linux

    // Start processing.
    mediaPlayer.startMedia(mrl);

    BufferedImage bufImg;
    for (int i = 0; i < 1000; ++i) {
        bufImg = mediaPlayer.getSnapshot();

        // Do something with BufferedImage...
        // ...
    }

    // Stop processing.
    mediaPlayer.stop();

    // Finish program.
    mediaPlayer.release();
    mediaPlayerFactory.release();
}