Saturday, February 8, 2014

Controlling VLC media player using JAVA

         Basically i was looking for a method to communicate with the VLC media player so that i can get the currently playing video on the player and my app can search the internet and get the details about the movie.For that i googled a lot and finally came to know that VLC has a web interface and i used that with my JAVA app to control and getting details of the movie.
           
        First open the VLC and goto tools->preferences and check the All radio button. and navigate to the Main Interface and click on lua and there you have to set HTTP password (Otherwise u cannot access the web interface). Screenshot is given below.

           


 Now lets clear some security issues on windows to allow us to control the vlc from any PC in the network.Goto Control Panel click on Allow a program through Windows Firewall.
 

      And now click allow another program and browse to VLC.exe and check on as shown below.


       Now open any browser and navigate to 127.0.0.1:8888  you will see a interface as below. Before that in VLC player set the web interface on.That's it now you can control the Player from browser.Now comes the JAVA Part.

Code :

package com.oksbwn.Y2014.vlcInteraction;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class vlcStatus {
    String time;
    String name;
    String state;
    String length;
    public static void main(String[] args){
        vlcStatus vs=new vlcStatus();
        replace r=new replace();
         String mov=vs.getName();
        System.out.println(r.replaceIt(mov));
    }
    public  vlcStatus()
    { 
     try
      { 
         String urlString = "http://127.0.0.1:8080/requests/status.xml";
         Authenticator.setDefault(new authoRize());
         URL url = new URL(urlString);
         InputStream content = (InputStream) url.getContent();
      
        
       DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
       DocumentBuilder b = f.newDocumentBuilder();
       Document doc = b.parse(content);
       doc.getDocumentElement().normalize();
                // loop through each item
      NodeList items = doc.getElementsByTagName("root");
      Element e = (Element)items.item(0);
    
      time=e.getElementsByTagName("time").item(0).getChildNodes().item(0).getNodeValue();
      length=e.getElementsByTagName("length").item(0).getChildNodes().item(0).getNodeValue();
      state=e.getElementsByTagName("state").item(0).getChildNodes().item(0).getNodeValue();
      
      NodeList items1= e.getElementsByTagName("information");
      Element f1 = (Element)items1.item(0);
      
      NodeList items11= f1.getElementsByTagName("category");
      Element f111 = (Element)items11.item(0);
      try{
          name=f111.getElementsByTagName("info").item(2).getChildNodes().item(0).getNodeValue();
          }catch(Exception e1){
              
                  try{
                      name=f111.getElementsByTagName("info").item(1).getChildNodes().item(0).getNodeValue();
                      }catch(Exception e11){
                          try{
                              name=f111.getElementsByTagName("info").item(0).getChildNodes().item(0).getNodeValue();
                              }catch(Exception e2){
                                  try{
                                      name="oksbwn";
                                      }catch(Exception e111){}
                              }
                      }
          }
    
             
      } catch (Exception e)
           {//new handleExceptions(e);
      }
    
    }
    public String getName(){return this.name;}
    public String getTime(){return this.time;}
    public String getedState(){return this.state;}
    public String getLength(){return this.length;}
}

And the CODE for the authorization is.
Code:

package com.oksbwn.Y2014.vlcInteraction;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

class authoRize extends Authenticator {
    private String username, password;

    public authoRize() {
      username = "";
      password = "om";
    }
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password.toCharArray());
    }
  }


Thanks...