Play a video with vlcj on a Mac - vlc

Hi I'm trying to get a video running with vlcj on a mac.
I tried several things but everywhere there is something wrong.
The Programm is starting up, but there is no video playing. Does anyone know how to handle vlcj on a mac?
My intentions are to play a rtsp stream on a mac with java. I tried it first with opencv and now vlcj but didn't succed.
Are there any good examples how to do that on a mac or does anyone know why it is not working?
Thanks
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class VideoExample {
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
public static void main(final String[] args) {
uk.co.caprica.vlcj.binding.LibC.INSTANCE.setenv("VLC_PLUGIN_PATH", "/Applications/VLC.app/Contents/MacOS/plugins", 1);
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new VideoExample(args);
}
});
}
private VideoExample(String[] args) {
JFrame frame = new JFrame("vlcj Tutorial");
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
frame.setContentPane(mediaPlayerComponent);
frame.setLocation(100, 100);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayerComponent.getMediaPlayer().playMedia("/test.m4v");
}
}
Bug:
[main] INFO uk.co.caprica.vlcj.Info - vlcj: <version not available>
[main] INFO uk.co.caprica.vlcj.Info - java: 1.8.0_45 Oracle Corporation
[main] INFO uk.co.caprica.vlcj.Info - java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
[main] INFO uk.co.caprica.vlcj.Info - os: Mac OS X 10.10.4 x86_64
[AWT-EventQueue-0] INFO uk.co.caprica.vlcj.binding.LibVlcFactory - vlc: 2.2.1 Terry Pratchett (Weatherwax), changeset 2.2.1-0-ga425c42
[AWT-EventQueue-0] INFO uk.co.caprica.vlcj.binding.LibVlcFactory - libvlc: /Applications/VLC.app/Contents/MacOS/lib/libvlc.dylib
JavaVM WARNING: JAWT_GetAWT must be called after loading a JVM
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Can't load JAWT
at com.sun.jna.Native.getWindowHandle0(Native Method)
at com.sun.jna.Native$AWT.getComponentID(Native.java:1879)
at com.sun.jna.Native.getComponentID(Native.java:253)
at uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface.attach(CanvasVideoSurface.java:76)
at uk.co.caprica.vlcj.player.embedded.DefaultEmbeddedMediaPlayer.attachVideoSurface(DefaultEmbeddedMediaPlayer.java:162)
at uk.co.caprica.vlcj.player.embedded.DefaultEmbeddedMediaPlayer.onBeforePlay(DefaultEmbeddedMediaPlayer.java:327)
at uk.co.caprica.vlcj.player.DefaultMediaPlayer.play(DefaultMediaPlayer.java:804)
at uk.co.caprica.vlcj.player.DefaultMediaPlayer.playMedia(DefaultMediaPlayer.java:265)
at uk.co.caprica.vlcj.player.DefaultMediaPlayer.playMedia(DefaultMediaPlayer.java:256)
at VideoExample.<init>(VideoExample.java:46)
at VideoExample.<init>(VideoExample.java:34)
at VideoExample$1.run(VideoExample.java:29)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

This is a well-known and well-documented issue with vlcj (and in fact other applications) on OSX with a JDK later than 1.6.
I know link-only answers are not the best, but there's way too much information on this problem to repeat here, so check https://github.com/caprica/vlcj/issues/205.
Essentially you can't use the embedded media player component since it relies on a heavyweight AWT component (a Canvas) to 'host' the video, and in JDK 1.7 and later on OSX there is no heavyweight AWT anymore.
So on OSX either use the so-called "direct" rendering approach with vlcj, or use JDK 1.6.

Thanks
Here is everything explained step by step
http://capricasoftware.co.uk/#/projects/vlcj/tutorial/direct-rendering
the rtsp stream works perfectly fine if you are ok with 3 seconds delay

Related

Using JavaCV with Kotlin

I'm currently attempting to start a project using JavaCV in Kotlin. I'm using IntelliJ Idea as my IDE. I'm using JavaCV 1.3.2 and OpenCV 3.20.
This is my setup for the module's dependancies for OpenCV:
and for JavaCV:
I have opencv before javacv in the dependancies.
To test that OpenCV is present and valid, I wrote the below to test. As I can loadLibrary and the version number shows correctly I can assume that OpenCV is actually working.
import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.CvType
import org.opencv.core.Scalar
import org.bytedeco.javacv.OpenCVFrameGrabber
fun main(args : Array<String>) {
println("Test Built Successfully")
System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
println("Running OpenCV Version ${Core.VERSION}")
val grabber = OpenCVFrameGrabber(1)
}
The line where we initialize the grabber rasies the following exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_core in java.library.path
Caused by: java.lang.UnsatisfiedLinkError: no opencv_imgproc320 in java.library.path
Perhaps I am missing something here, but I have followed the instructions provided by the README.md in the Git repository for JavaCV. I haven't seen anybody else trying to use this library with Kotlin on StackOverflow, though have checked out some posts about the same exception being raised using java.
Handling this error is no different in Kotlin from Java; you need to specify the path to the native libraries for opencv. By default on Windows it will look for the native libraries in whatever is set in your PATH environment variable.
You can also explicitly specify which directory to look for the native libraries by specifying the system property java.library.path (as indicated by the error message).
For example, you can add a run configuration like this:
Where ${PATH_TO_DYNAMIC_LIB} would be where ever the native lib opencv_imgproc320.dll is - I think in your case it would be C:/Users/ms/IdeaProjects/CVTest/opencv/build/java/x64.
I was able to resolve this by leaving my VM options blank and adding all necessary OpenCV libraries as native library paths in my JavaCV library configuration.
on MacOS, you don't need to install a native library. On OpenCV3.41, as fetched from
<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.4.2-1</version>
</dependency>
I looked in the library (jar tf ~/.m2/repository/org/openpnp/opencv/3.4.2-1/opencv-3.4.2.-1.jar) and found dlls and whatnot in it:
jar tf *1.jar | grep nu
...
nu/pattern/opencv/osx/
nu/pattern/opencv/osx/x86_64/
nu/pattern/opencv/osx/x86_64/README.md
nu/pattern/opencv/osx/x86_64/cmake.log
nu/pattern/opencv/osx/x86_64/libopencv_java342.dylib
...
This led me to this StackOverflow question on nu.pattern which show how to use the nu.pattern in code.
static {
nu.pattern.OpenCV.loadShared();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
}
This prologue code enabled sample apps which used to fail as above to run.

From axis2 function call, which is in another jar is not called and no error also shown

I have recently migrated my environment from Java 6 to Java 7. Installed Netbeans 8.0.2. A Grails(Version 2.1.1) project is running in it.
From Grails project using withREST a web service is called in another Tomcat (Version 8). This tomcat(version 8) is using axis2 to expose the methods. A java class "OneWebservice.java". Built as aar file and deployed.
Code is like this.
import com.project.Two.TwoFunction;
import com.project.Three.ThreeFunction;
public class OneWebservice{
function getOneandTwo()
{
com.project.Two.TwoFunction();
.....
com.project.Three.ThreeFunction();
}
}
When function getOneandTwo() is called, in the above code Two.TwoFunction is called, while Three.ThreeFunction is not called. No error is also printed.
Ok, Got this fixed.
We have Solr implemented for text based search.
Import related to Solr was there in com.project.Three.ThreeFunction this class file
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
was giving problem, if this is commented its working fine...
I think Solr is not working in Java 8 and Tomcat 8

Grails 2.3.0 Restful Service Runtime Exception

I´m currently running Grails 2.3.0 and Java jdk1.7.0_40 on a Windows 7 machine, no IDE. I wanted to see how the new Rest functionality looks like. So I created the domain class as follows:
package fergara
import grails.rest.*
#Resource(uri='/Cars', formats=['json', 'xml'])
class Car {
String model
static constraints = {
}
}
The error I got when open the url:
URI
/FergaraTest/Cars/1 Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'fergara.CarController#12c43f7' with class 'fergara.CarController' to class 'org.codehaus.groovy.grails.plugins.web.api.CommonWebApi'
I feel that the Oracle´s Java SDK is the problem. To make the app run I have to pass "no-reloading" to the grails run-app command, otherwise I get an error saying that it could not start the VM and cannot open the springloaded-core-1.1.3.jar.
A have the Grails 2.3.0 installed on an Ubuntu 13.04 with OpenJDK6 and it is working good.
Hope someone can shed a light on this. Thank you.

JAVACV : Webcam capturing using javacv

Tried to capture from a webcam using the JAVACV library. But it throws an UnsatisfiedLinkError.
Stack trace is as follows:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
C:\Users\anooj\AppData\Local\Temp\javacpp7955905460040\jniopencv_core.dll: %1 is not a valid Win32 application
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1928)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1825)
at java.lang.Runtime.load0(Runtime.java:792)
at java.lang.System.load(System.java:1059)
at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:418)
at com.googlecode.javacpp.Loader.load(Loader.java:368)
at com.googlecode.javacpp.Loader.load(Loader.java:315)
at com.googlecode.javacv.cpp.opencv_core.<clinit>(opencv_core.java:131)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.googlecode.javacpp.Loader.load(Loader.java:334)
at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:96)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.googlecode.javacpp.Loader.load(Loader.java:334)
at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:91)
at com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:170)
at javaapplication16.JavaApplication16.captureframe(JavaApplication16.java:24)
at javaapplication16.JavaApplication16.main(JavaApplication16.java:38)
Java Result: 1
Our code is below:
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
public class JavaApplication16 {
/**
* #param args the command line arguments
*/
public static void captureframe()
{
OpenCVFrameGrabber grabber=new OpenCVFrameGrabber(0);
try
{
grabber.start();
IplImage img=grabber.grab();
if(img!=null)
{
cvSaveImage("capture.jpg", img);
}
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
public static void main(String[] args) {
captureframe();
}
}
Make Sure you have installed Microsoft Visual C++ Redistributable
package and setup path variable correctly. Below link explains all
the necessary steps
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html
Use VideoInputFrameGrabber instead of OpncvFrameGrabber as suggested
in link below
http://code.google.com/p/javacv/wiki/Windows7AndOpenCV#I_cannot_capture_from_my_Webcam_using_opencv_highgui_or_OpenCVFr
Remember that the version of openCV and javaCV must match.
Download latest version openCV 2..46 http://opencv.org/downloads.html
and download the javacp https://code.google.com/p/javacv/downloads/list [Note: choose the javacv-0.6-cppjars.zip 85.0 MB]
In this case you will not get Exception in thread "main" java.lang.UnsatisfiedLinkError:
My Code is running successfully.
see this link to solve the error you got Link1
see this link for the program to capture the image from camera, save it in a JPG format and show it on a canvas Link2
hope it solves your query.

javac will not compile enum, ( Windows Sun 1.6 --> OpenJDK 1.6)

package com.scheduler.process;
public class Process {
public enum state {
NOT_SUBMITTED, SUBMITTED, BLOCKED, READY, RUNNING, COMPLETED
}
private state currentState;
public state getCurrentState() {
return currentState;
}
public void setCurrentState(state currentState) {
this.currentState = currentState;
}
}
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.state;
public class Machine {
com.scheduler.process.Process p = new com.scheduler.process.Process();
state s = state.READY; //fails if I don't also explicitly import Process.state
p.setCurrentState(s); //says I need a declarator id after 's'... this is wrong.
p.setCurrentState(state.READY);
}
Modified the example to try and direct to the issue. I cannot change the state on this code. Eclipse suggests importing Process.state like I had on my previous example, but this doesn't work either. This allows state s = state.READY but the call to p.setCurrentState(s); fails as does p.setCurrentState(state.READY);
Problem continued.... Following Oleg's suggestions I tried more permutations:
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.*;
public class Machine {
com.scheduler.process.Process p = new com.scheduler.process.Process();
public state s = Process.state.READY;
p.setCurrentState(s);
p.setCurrentState(state.READY);
}
Okay. It's clear now that I'm a candidate for lobotomy.
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.state;
public class Machine {
public void doStuff(){
com.scheduler.process.Process p = new com.scheduler.process.Process();
state s = state.READY; //fails if I don't also explicitly import Process.state
p.setCurrentState(s); //says I need a declarator id after 's'... this is wrong.
p.setCurrentState(state.READY);
}
}
I needed to have a method in the class--but we're still missing something (probably obvious) here. When I go via the command line and run javac on the Machine class AFTER compiling Process, I still get the following error:
mseil#context:/media/MULTIMEDIA/Scratch/Scratch/src/com/scheduler/machine$ javac Machine.java
Machine.java:3: package com.scheduler.process does not exist
import com.scheduler.process.Process;
^
So I guess the question now becomes, what idiot thing am I missing that is preventing me from compiling this by hand that eclipse is doing for me behind the scene?
======
Problem solved here:
Java generics code compiles in eclipse but not in command line
This has just worked for me:
Download latest Eclipse
Create new project
Create two packages com.scheduler.process and com.scheduler.machine
Create class Process in package com.scheduler.process and class Machine in com.scheduler.machine and copy their contents from your post modifying them to conform to Java language syntax, like this:
Everything compiles right away.
------ to answer the previous version of the question ------
To answer the question as it is right now: you need to either
import com.scheduler.process.Process.status or import com.scheduler.process.Process.* and refer to status as just status
or
import com.scheduler.process.* or import com.scheduler.process.Process and refer to status as Process.status
------ to answer the original version of the question ------
You can't import classes that are not inside some package. You just can't. It is a compile time error to import a type from the unnamed package.
You don't need to import anything if your classes are in the same package, or if all of your classes are packageless.
If Process class was inside some package it would be possible to import just its status inner class: import a.b.c.Process.status would work just fine.
All your Windows/Linux migration issues don't have anything to do with Java and exceptions that you see. import Process.state; will produce exception on any OS because you can't import classes that don't belong to any package.
Eclipse doesn't use the Sun JDK by default. I would assume that you are using Eclipse's built in compiler as Sun's JDK and the OpenJDK are almost identical.
Java code compiles and runs exact the same on Windows and Linux most of the time (unless you use a few of the platform specific operations)
I suspect you are not building the code the same way and when you compile Machine, the Process class has not been compiled.
I suggest you use a standard build system like maven or ant and it will build the same everywhere. Failing that run Eclipse on Linux or just the same .class you use on windows as they don't need to be re-compiled in any case.
BTW: You don't need to import Process.state as it not used and its in the same package (so you wouldn't need to if you did)

Resources