Search code examples
javavideovlcj

VLCJ doesn't play video when using getClass().getResource()


I'm making a game, but it depends on videos; I hadn't worked before with videos so I used VLCJ as it was the first thing that popped up in YouTube. Anyway, the thing is that when I use getClass().getResource() and I execute the jar, it doesn't load the media. So what I need, is that the media can be opened from a jar.

String path1 = getClass().getResource("/media/introVideo.mp4").getFile(); //Not works (I want this to work)

String path2 = getClass().getResource("/media/introVideo.mp4").toExternalForm());  //Not works (I want this to work)

String path3 = getClass().getResource("/media/introVideo.mp4").toString());  //Not works (I want this to work)

String path4 = getClass().getResource("/media/introVideo.mp4").getFile().replace("/", "\\"); //Works (Not what I need)

String path5 = new File("src\\media\\introVideo.mp4").toString(); //Works (Not what I need)

The error that I get when I use path1, path2 & path3 are the following:

libdvdnav: Using dvdnav version 6.0.0
libdvdread: Could not open D:\Programming\HyperDance\\D:\Programming\HyperDance\build\classes\media\introVideo.mp4 with libdvdcss.
libdvdread: Can't open D:\Programming\HyperDance\\D:\Programming\HyperDance\build\classes\media\introVideo.mp4 for reading
libdvdnav: vm: failed to open/read the DVD
[000000001c5ed6a0] filesystem stream error: cannot open file D:\Programming\HyperDance\\D:\Programming\HyperDance\build\classes\media\introVideo.mp4 (Invalid argument)
[000000001c5e3d00] main input error: Your input can't be opened
[000000001c5e3d00] main input error: VLC is unable to open the MRL 'file:///D:/Programming/HyperDance/%2FD%3A%2FProgramming%2FHyperDance%2Fbuild%2Fclasses%2Fmedia%2FintroVideo.mp4'. Check the log for detail

Snippet:

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.Canvas;
import java.awt.Color;
import javax.swing.JFrame;
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.player.embedded.windows.Win32FullScreenStrategy;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class Test extends JFrame {

  static Test frame;
  static Canvas canvas;

    public Test() {
        canvas = new Canvas();
          canvas.setBackground(Color.BLACK);
        add(canvas);
    }

    public static void main(String[] args) {
        frame = new Test();
          frame.setSize(1047,615);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        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(frame));
            emp.setVideoSurface(mpf.newVideoSurface(canvas));
          //↓ How do I fix the importing of the media ↓
            emp.prepareMedia(new Object().getClass().getResource("/media/introVideo.mp4").getFile());
          //↑ The parameter only accepts Strings ↑
            emp.play();
    }
}

Summing up:

  • I need to play a video from a jar.

  • The video is played when I don't use getClass().getResource(), but I need to use it to access the media from the jar.

  • The question: How do I play a video using VLCJ from a jar?


Solution

  • Well, I already found the solution:

    • First, I put the jar inside the folder Surprise along another folder that contains the video.

    • Then, I make the program search for a text file guide.txt that is inside the jar to know the location of my Surprise folder.

    • Finally, I replaced the guide.txt path with the path of the video:

      C:/Users/Steve/Desktop/Surprise/ HyperDance-BTS.jar! /media/guide.txt

      -> C:/Users/Steve/Desktop/Surprise/ videos /introVideo.mp4

    Folders:

    • Surprise (C:, D:, whatever disc:)
      • HyperDance-AC/DC.jar
        • Guide.txt
        • Media (images and sounds)
      • Videos(Hidden folder)
        • introVideo.mp4

    Snippet:

    import com.sun.jna.Native;
    import com.sun.jna.NativeLibrary;
    import java.awt.Canvas;
    import java.awt.Color;
    import javax.swing.JFrame;
    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.player.embedded.windows.Win32FullScreenStrategy;
    import uk.co.caprica.vlcj.runtime.RuntimeUtil;
    
    public class Test extends JFrame {
    
      static Test frame;
      static Canvas canvas;
      static int video = 1;
    
        public Test() {
            canvas = new Canvas();
              canvas.setBackground(Color.BLACK);
            add(canvas);
        }
    
        public static void main(String[] args) {
            frame = new Test();
              frame.setSize(1047,615);
              frame.setLocationRelativeTo(null);
              frame.setResizable(false);
              frame.setVisible(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            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(frame));
                emp.setVideoSurface(mpf.newVideoSurface(canvas));
    
                if (video==1) {
                    emp.prepareMedia(url("introVideo.mp4"));
                    emp.play();
                }
                if (video==2) {
                    //Another video
                }
        }
    
        static String url (String video) {
            String mrl = new Object().getClass().getResource("/media/guide.txt").getFile();
            String url = mrl.replace("/", "\\").split("\\\\",2)[1].split("Surprise")[0].concat("Surprise\\videos\\"+video);
              return url;
        }
    }
    

    Is there a better way or a more official one to do this?