我正在制作游戏,但这取决于视频;我以前没看过视频,所以我使用VLCJ,因为这是YouTube上出现的第一件事。无论如何,问题是当我使用getClass().getResource()
并执行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)
使用path1,path2和path3时出现的错误如下:
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();
}
}
总结:
我需要从罐子中播放视频。
当我不使用getClass().getResource()
时播放视频,但是我需要使用它来从jar中访问媒体。
问题:如何使用VLCJ从罐子中播放视频?
嗯,我已经找到了解决方法:
首先,将jar放在另一个包含视频的文件夹的Surprise
文件夹中。
然后,我让程序搜索jar内的文本文件guide.txt
以了解我的Surprise
文件夹的位置。
最后,我用视频的路径替换了guide.txt
路径:
C:/ Users / Steve / Desktop / Surprise / HyperDance-BTS.jar! /media/guide.txt
-> C:/用户/史蒂夫/桌面/惊喜/ 视频 /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;
}
}
是否有更好的方法或更正式的方法?