Add subtitle and basic operation panel in VLCJ # Java - java

Good day colleagues!
I had several trouble using VLCJ and other Java media API-s.
1) I'd add a simple *.srt file to my EmbeddedMediaPlayerCompononent, bot how is this possible?
2) Also, how can I configurate the VLC lib in an x64 Windows OS?
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),libVlc.class);
This not works well.
3) How could I add basic operation interface to my EmbeddedMediaPlayerCompononent like pause/play button?
Thank you, best regards! :)
My "VideoPlayer" class
package GUI.MediaPlayer;
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import StreamBean.UIBean;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class VideoPlayer extends JFrame{
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
public VideoPlayer(String videoURL) {
String os = System.getProperty("os.name").toLowerCase();
if(os.startsWith("win")){
String registrytype = System.getProperty("sun.arch.data.model");
System.out.println("a rendszered : " +os+" - " +registrytype+ " bites");
if(registrytype.contains("32")){
//Windows 32 bites verzió
System.out.println("Belépett a 32-be");
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}else if(registrytype.contains("64")){
//Windows 64 bites verzió
System.out.println("Belépett a 64-be");
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}else{
JOptionPane.showMessageDialog(null, "Kérem előbb telepítse a VLC lejátszót.");
}
}
if(os.startsWith("mac")){
//Mac OSX kiadáshoz
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib/");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
this.setTitle("Aktuális videó");
this.setLayout(new BorderLayout());
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
this.add(mediaPlayerComponent,BorderLayout.CENTER);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
//set the Jframe - this - resolution to the screen resoltuion
new UIBean().setWindowSize(this);
this.setVisible(true);
mediaPlayerComponent.getMediaPlayer().playMedia(videoURL);
}
}

To set an external subtitle file:
mediaPlayerComponent.getMediaPlayer().setSubTitleFile("whatever.srt");
How you add a pause/play button is entirely up to you, it requires standard Swing code that is not particular to vlcj. You add buttons to your user interface, and link those buttons up with the media player by using event listeners. For example, this is one way:
JButton playButton = new JButton("Play/Pause");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mediaPlayerComponent.getMediaPlayer.pause();
}
});
There are numerous reasons why the native library might not be found, but NativeLibrary.addSearchPath(...) certainly does work. You have to be sure you match the CPU architectures of your JVM and your VLC installation (32-bit JVM requires 32-bit VLC, 64-bit JVM requires 64-bit VLC). In most cases you should just use:
new NativeDiscovery().discover();
There are a whole bunch of step-by-step tutorials at http://capricasoftware.co.uk/#/projects/vlcj/tutorial

Focusing on the "basic operation interface" aspect of your question, note that EmbeddedMediaPlayerComponent extends Panel, an AWT component. Accordingly, the VLCJ overlay example shown here overrides paint(). This related, stand-alone example illustrates hit testing in such context.

Related

VLCJ-Pro Java returned: -57005

My task is to play multiple videos with a linked time scrubber in a grid, and I've gotten it to work (WITH VLCJ NOT VLCJ-PRO), but it is VERY finicky.
So I decided to give VLCJ-Pro a try, but I'm getting an error on the first line.
22:25:25.614 [main] INFO u.c.c.vlcj.discovery.NativeDiscovery - Discovery
found libvlc at 'D:\VLC'
C:\Users\trans\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: -57005
BUILD FAILED (total time: 0 seconds)
VLCJ-Pro is supposed to help with VLCJ's multi-video problems (native library crashes happen alot). So I figured I'd see if it helped with stability, but I can't even get it to run.
VLCJ-Pro Download Location
Here is my entire code I'm using to test the library. I'm using Netbeans as my IDE and I've added ALL the JAR libraries in the example code.
If you have any experience with VLCJ-Pro I would greatly appreciate any feedback on how I'm going wrong.
package vlcjprodemo;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.version.LibVlcVersion;
import uk.co.caprica.vlcjpro.client.player.OutOfProcessMediaPlayer;
import uk.co.caprica.vlcjpro.client.player.OutOfProcessMediaPlayerComponent;
import uk.co.caprica.vlcjpro.client.player.OutOfProcessMediaPlayerComponentFactory;
public class VLCJProDemo {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new NativeDiscovery().discover();
LibVlcVersion.getVersion();
//CRASHES HERE
OutOfProcessMediaPlayerComponentFactory theFactory = new OutOfProcessMediaPlayerComponentFactory();
OutOfProcessMediaPlayerComponent theComponent = theFactory.newOutOfProcessMediaPlayerComponent();
Canvas theVideoCanvas = new Canvas();
theVideoCanvas.setFocusable(true);
theVideoCanvas.setSize(new Dimension(1920, 1080));
theVideoCanvas.setLocation(0, 0);
theComponent.setVideoSurface(theVideoCanvas);
OutOfProcessMediaPlayer theMediaPlayer = theComponent.mediaPlayer();
theMediaPlayer.setRepeat(true);
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setBackground(Color.black);
mainFrame.setMaximumSize(new Dimension(1920, 1080));
mainFrame.setPreferredSize(new Dimension(1920, 1080));
mainFrame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.add(theVideoCanvas);
mainFrame.pack();
mainFrame.setVisible(true);
theMediaPlayer.playMedia("horse.avi");
}
}
Probably you are trying to use a trial/demo version of vlcj-pro that has expired.
If so, you need to wait until a new trial version is published.

Using an icon image for a GUI

I have created the following code for a school project, a "password protector", just for fun, really. However, the problem I have is that the icon image does not appear, but instead the default java "coffee cup".
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserInterfaceGUI extends JFrame
{
private static final long serialVersionUID = 1;
private JLabel userNameInfo; // ... more unimportant vars.
public UserInterfaceGUI()
{
this.setLayout(new FlowLayout());
userNameInfo = new JLabel("Enter Username:"); // ... more unimportant var. declartions
this.add(userNameInfo); // ... more unimportant ".add"s
event e = new event();
submit.addActionListener(e);
}
public static void main(String[] args)
{
//This icon has a problem \/
ImageIcon img = new ImageIcon("[File Location hidden for privacy]/icon.ico");
UserInterfaceGUI gui = new UserInterfaceGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(400, 140);
gui.setIconImage(img.getImage());
gui.setTitle("Password Protector");
gui.setVisible(true);
}
}
Can someone tell me why this just shows the java coffee cup at the bottom of the screen and on the bar at the top of the window?
There are two likely problems here:
Java is unlikely to support .ico files. The only types that can be relied on are GIF, PNG & JPEG. For all types supported on any specific JRE, use ImageIO.getReaderFileSuffixes() (but seriously, for app. icons stick to the 3 types with guaranteed support).
The code is trying to load an application resource as a file, when it will likely be (or become) an embedded-resource that should be accessed by URL. See the embedded resource info. page for tips on how to form the URL.

VLCJ NullPointer (I just want a simple cross-platform java video player)

I am wanting to make a simple java applicaiton to play video. I want it to play mpeg4 and mov formats in particular. JMF is what I started with and I have a lovely working example. However, there is no support for mov or mpeg4 formats. I've looked at Xuggler but can't see a SIMPLE way to get it working. VLCJ seemed easy - I downloaded the jar files and attached them to my project (vlcj-2.1.0.jar, jna-3.4.0.jar, platform-3.4.0.jar, vlcj-2.1.0.jar)). I got the sample code and adapted it (below). But when I run the code, I get a java.lang.NullPointerException exception. I've tried adjusting the number and direciton of the slashes (forward and backward) in the filename. Nothing seems to work. Please could you help???
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import java.lang.Object;
import uk.co.caprica.vlcj.mrl.FileMrl;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class TestPlayer {
private final JFrame frame;
private EmbeddedMediaPlayerComponent mediaPlayer;
public static void loadLibs(){
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC/"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
public static void main(final String[] args){
loadLibs();
final String mrl = "file://C:/Test.mov";
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPlayer().run(mrl);
}
});
}
public TestPlayer(){
frame = new JFrame("test VLCJ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100,100);
frame.setSize(600,400);
frame.setVisible(true);
}
private void run(String mrl){
System.out.println(mrl);
try{
mediaPlayer.getMediaPlayer().playMedia(mrl);
}catch(Exception e){
System.err.println(e.toString());
}
}
}
I'm using VLC version 2.0.2 and VLCJ 2.1.0 sources and JDK 1.7 on windows 32 bit. I hope it's something simple...
It looks like you are using mediaPlayerwithout ever initializing it, thus causing a NullPointerException in run().
Try initializing it in your constructor.

Can I add a Jython action listener to a java program?

I have a Java program which I'd like to call from a Jython program. I'd like to be able to have the two interface with one another and had hoped to call the Java program from Jython with an action listener as a parameter, but so far I have had no luck.
Jython code:
import sys
sys.path.append("sgJython.jar")
from java.awt.event import ActionListener
from java.awt.event import ActionEvent
from sg.gui import MainGui
class MyListener(ActionListener):
def ActionPerformed(e):
print("gotit")
ml = MyListener()
MainGui(ml)
Java code:
package sg;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class MainGui
{
public MainGui(ActionListener listener)
{
top = new JFrame();
top.setBounds(300, 300, 600, 300);
JButton doneButton = new JButton("Done");
doneButton.addActionListener(listener);
top.add(doneButton)
}
}
Is there some other way of doing this? If not, is there a better way to go about integrating Java and Python into a single application, or is that not a good thing to do?
The way it is already works, you just have to fix the actionPerformed method, which has a wrong signature and which does not declare the self parameter.
class MyListener(ActionListener):
def actionPerformed(self, e):
print("gotit")
In your Java code, you missed a couple of semicolons and since you are not displaying the frame it will not be visible unless you explicitly ask it from your Jython code. So I changed it a bit for testing purposes:
public class MainGui {
private JFrame top;
public MainGui(ActionListener listener) {
top = new JFrame();
top.setBounds(300, 300, 600, 300);
JButton doneButton = new JButton("Done");
doneButton.addActionListener(listener);
top.add(doneButton);
top.pack();
top.setVisible(true);
}
}
I did the Java code in a Java Project in Eclipse and the Python code in PyDev project in Eclipse, then I ran the Python script and it worked like a charm.

Using music in a java program

I was trying out the method of creating a background music for a java program, but it displayed an IO excedption error when i clicked the play button.
package javaentertainment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.*;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Music
{
public static void main(String args[])
{
JFrame frame=new JFrame();
frame.setSize(100,100);
JButton button=new JButton("P L A Y");
frame.add(button);
button.addActionListener(new AL());
frame.show();
}
public static class AL implements ActionListener
{
public void actionPerformed(ActionEvent e) {
music();
}
}
public static void music()
{
AudioPlayer MGP=AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinousAudioDataStream loop=null;
try
{
BGM = new AudioStream(new FileInputStream("Vision.wmv"));
MD=BGM.getData();
loop=new ContinousAudioDataStream(MD);
}
catch (IOException ex)
{
System.out.println(ex);
}
MGP.start(loop); // word loop was underlined by netbeans
}
}
When I run the program and click on play it displays the following error,
java.io.IOException: could not create audio stream from input stream
You should use JMF (Java Media Framework). For your interest: The list of accepted formats can be found here.
In short, it supports AIFF, AVI, GSM, MVR, MID, MPG, MP2, MOV, AU and WAV files.
But there is a workarond as stated here:
On a side note, if you add a
mime-setting in JMFRegistry to map
Windows Media content (such as .asf
and .wmv) to the content-type
"video/mpeg", JMF can actually play
Windows Media or any other DirectShow
file (and only file - http wont work).
I would be surprised if Java can hand Windows Media format samples - try converting the .wmv to a .wav file and see if it works then.
Just got this, as well.
java.io.IOException: could not create AudioData object
Appears from the source [1] that this means that "your audio file is size > 1 MB" and it doesn't like that for whatever reason. Maybe a bug [?] that they don't accomodate for this.
One work-around might be to use JMF instead, as suggested, if you want looping to work for large files anyway.
[1] http://www.docjar.com/docs/api/sun/audio/AudioStream.html#getData

Categories

Resources