如何在Java中使用vlcj?

huangapple go评论61阅读模式
英文:

how to use vlcj with java?

问题

package mrbool.vlc.example;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;

import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.binding.LibVlc;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.binding.RuntimeUtil;

public class JavaApplication1 {
    
    public static void main(String[] args) {
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        MediaPlayerFactory factory = new MediaPlayerFactory();
    }
}
英文:

Hello Everyone i'm trying to use vlcj for java but am running into a lot of errors. I checked my jvm version and vlc media version both are 64 bit.

I tried a lot of codes that I had researched in the internet. I followed the step by inserting vlcj.jar in my code but nothing seems to work.

I followed the tutorial on caprica but it wouldn't work. Now i'm getting error

Exception in thread "main" java.lang.IllegalArgumentException: Interface (LibVlc) of library=libvlc does not extend Library B.

Can someone please help on this?

package mrbool.vlc.example; 

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;

import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.binding.LibVlc;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.binding.RuntimeUtil;
public class JavaApplication1 {
        


    public static void main(String[] args) {
      NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\\Program Files\\VideoLAN\\VLC");
      Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
      MediaPlayerFactory factory = new MediaPlayerFactory();
    }

}

答案1

得分: 2

将 VLC 媒体播放器安装到与您的项目位于相同的目录中。

英文:

install vlc media player in to same directory that your project is located

答案2

得分: 0

有时问题是由于VLC和JRE架构不兼容所致。

您可以使用以下代码检查JRE架构:

public class JavaApplication12 {
    public static void main(String[] args) {
        System.out.println(System.getProperty("sun.arch.data.model"));
    }
}
EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
EmbeddedMediaPlayer embeddedMediaPlayer = mediaPlayerComponent.getMediaPlayer();

Canvas videoSurface = new Canvas();
videoSurface.setBackground(Color.black);
videoSurface.setSize(800, 600);

List<String> vlcArgs = new ArrayList<String>();

vlcArgs.add("--no-plugins-cache");
vlcArgs.add("--no-video-title-show");
vlcArgs.add("--no-snapshot-preview");

MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(vlcArgs.toArray(new String[vlcArgs.size()]));
mediaPlayerFactory.setUserAgent("vlcj test player");
embeddedMediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(videoSurface));
embeddedMediaPlayer.setPlaySubItems(true);

final PlayerControlsPanel controlsPanel = new PlayerControlsPanel(embeddedMediaPlayer);
PlayerVideoAdjustPanel videoAdjustPanel = new PlayerVideoAdjustPanel(embeddedMediaPlayer);

//            mediaPlayerComponent.getMediaPlayer().playMedia(Constant.PATH_ROOT + Constant.PATH_MEDIA + "tmp.mp4");
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setBackground(Color.black);
mainFrame.add(videoSurface, BorderLayout.CENTER);
mainFrame.add(controlsPanel, BorderLayout.SOUTH);
mainFrame.add(videoAdjustPanel, BorderLayout.EAST);

//创建一个按钮,单击时将隐藏该面板。
mainFrame.pack();
mainFrame.setVisible(true);

embeddedMediaPlayer.playMedia("tmp.mp4");
英文:

Sometimes the problem is due to incompatibility of the architecture of VLC and JRE.

You can check JRE architecture using the code below:

public class JavaApplication12 {
    public static void main(String[] args) {
        System.out.println(System.getProperty(&quot;sun.arch.data.model&quot;));
    }
}


EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
        EmbeddedMediaPlayer embeddedMediaPlayer = mediaPlayerComponent.getMediaPlayer();

        Canvas videoSurface = new Canvas();
        videoSurface.setBackground(Color.black);
        videoSurface.setSize(800, 600);

        List&lt;String&gt; vlcArgs = new ArrayList&lt;String&gt;();

        vlcArgs.add(&quot;--no-plugins-cache&quot;);
        vlcArgs.add(&quot;--no-video-title-show&quot;);
        vlcArgs.add(&quot;--no-snapshot-preview&quot;);

        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(vlcArgs.toArray(new String[vlcArgs.size()]));
        mediaPlayerFactory.setUserAgent(&quot;vlcj test player&quot;);
        embeddedMediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(videoSurface));
        embeddedMediaPlayer.setPlaySubItems(true);

        final PlayerControlsPanel controlsPanel = new PlayerControlsPanel(embeddedMediaPlayer);
        PlayerVideoAdjustPanel videoAdjustPanel = new PlayerVideoAdjustPanel(embeddedMediaPlayer);

//            mediaPlayerComponent.getMediaPlayer().playMedia(Constant.PATH_ROOT + Constant.PATH_MEDIA + &quot;tmp.mp4&quot;);
        JFrame mainFrame = new JFrame();
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setBackground(Color.black);
        mainFrame.add(videoSurface, BorderLayout.CENTER);
        mainFrame.add(controlsPanel, BorderLayout.SOUTH);
        mainFrame.add(videoAdjustPanel, BorderLayout.EAST);

        //create a button which will hide the panel when clicked.
        mainFrame.pack();
        mainFrame.setVisible(true);

        embeddedMediaPlayer.playMedia(&quot;tmp.mp4&quot;);

答案3

得分: 0

以下是您要翻译的内容:

"Also, to do this yourself I would suggest using chrome. You just right click on whatever you want to scrape and go to inspect element. It will take you to the exact spot in the html where that element is located. In this case you first want to find out where the root of all the result listings are. When you find that, you want to specify the element, and preferably an unique attribute to search it by. In this case the root element i

public class ScanWebSO
{
public static void main (String args[])
{
Document doc;
try{
doc = Jsoup.connect("https://www.google.com/search?as_q=&as_epq=%22Yorkshire+Capital%22+&as_oq=fraud+OR+allegations+OR+scam&as_eq=&as_nlo=&as_nhi=&lr=lang_en&cr=countryCA&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=").userAgent("Mozilla").ignoreHttpErrors(true).timeout(0).get();
Elements links = doc.select("li[class=g]");
for (Element link : links) {
Elements titles = link.select("h3[class=r]");
String title = titles.text();

        Elements bodies = link.select(&quot;span[class=st]&quot;);
        String body = bodies.text();

        System.out.println(&quot;Title: &quot;+title);
        System.out.println(&quot;Body: &quot;+body+&quot;\n&quot;);
    }
}
catch (IOException e) {
    e.printStackTrace();
}

}
}"

注意:上述文本中包含了HTML转义字符,这些字符可能需要进一步处理以在代码中使用。

英文:

Also, to do this yourself I would suggest using chrome. You just right click on whatever you want to scrape and go to inspect element. It will take you to the exact spot in the html where that element is located. In this case you first want to find out where the root of all the result listings are. When you find that, you want to specify the element, and preferably an unique attribute to search it by. In this case the root element i

public class ScanWebSO 
    {
    public static void main (String args[])
    {
        Document doc;
        try{
            doc =        Jsoup.connect(&quot;https://www.google.com/search?as_q=&amp;as_epq=%22Yorkshire+Capital%22+&amp;as_oq=fraud+OR+allegations+OR+scam&amp;as_eq=&amp;as_nlo=&amp;as_nhi=&amp;lr=lang_en&amp;cr=countryCA&amp;as_qdr=all&amp;as_sitesearch=&amp;as_occt=any&amp;safe=images&amp;tbs=&amp;as_filetype=&amp;as_rights=&quot;).userAgent(&quot;Mozilla&quot;).ignoreHttpErrors(true).timeout(0).get();
            Elements links = doc.select(&quot;li[class=g]&quot;);
            for (Element link : links) {
                Elements titles = link.select(&quot;h3[class=r]&quot;);
                String title = titles.text();
    
                Elements bodies = link.select(&quot;span[class=st]&quot;);
                String body = bodies.text();
    
                System.out.println(&quot;Title: &quot;+title);
                System.out.println(&quot;Body: &quot;+body+&quot;\n&quot;);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

huangapple
  • 本文由 发表于 2020年8月23日 23:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定