Wednesday, November 9, 2011

Controlling WINAMP from Java Program

Yes. It is possible to control Winamp media player from a Java program. This is very simple. Before we discuss the code let me show you a simple diagram for understanding the logic.

Java Navite Access is a project which provides access to Windows APIs using Java code. Writing programs using JNA is very very simple. Read More about JNA....

Using JNA we get the Java representation of the Winamp Window. Then send different message codes (one per operaion) to the window handle.

The Java code is self explainable:



import java.util.HashMap;
import java.util.Map;

import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.User32;

/**
 * This class controls the Winamp Media Player.
 * @author Santosh Pai
 */
public class WinampControl {

/**
* This is a map which stores the command values per operation.
*/
private static Map<String,String[]> h ;
static{
h = new HashMap<String,String[]>();
h.put("play", new String[]{"40045","0"});
h.put("stop", new String[]{"40047","0"});
h.put("pause", new String[]{"40046","0"});
h.put("next", new String[]{"40048","0"});
h.put("previous", new String[]{"40044","0"});
}

/**
* User32 DLL instance
*/
    public static User32 USER32INST;

  /**
* This method checks if the Winamp Window is currently present. i.e, Winamp is running.
* @param windowClass Name of the window class
* @param windowName Name of the window
* @return Return window handle in case winamp is running, else retun NULL.
*/
public static HWND isWinampRunning(){

HWND windowHandle = USER32INST.FindWindow("Winamp v1.x",null);

if(windowHandle != null)
  return windowHandle;
else{
System.out.println("WINAMP is not running.");
  return null;
}

}

/**
     * This method is used to send the control commands to Winamp. Before sending any command,
     * the method checks if the Winamp is open or not. If winamp is running the command
     * is sent using Windows User32 DLL sendMessage function.
     * @param windowClassName The name is used as Winamp v1.x
     * @param command Command to control the Winamp
     */
public static void sendCommandToWinamp(String command){
USER32INST = User32.INSTANCE;
//Check if the winamp is running
HWND winampHandle = isWinampRunning();

if(null != winampHandle){

   String[] values = h.get(command);
 
   WinDef.WPARAM wParamValue = new WinDef.WPARAM(Long.parseLong(values[0]));
 
   WinDef.LPARAM lParamValue = new WinDef.LPARAM(Long.parseLong(values[1]));

   //Send the command to the Winamp Window and control it
   USER32INST.PostMessage(winampHandle,273, wParamValue, lParamValue);

}

}

}

So now the Winamp is CONTROLLED :-) ...

I will post more code in future. I am working on a project "CONTROLLED".

1 comment:

  1. Hi, your codes are helpful. I tried to convert the jna class to run as Windows Services but it's not working. It's working fine if I run the java class from DOS prompt. Do you have any idea?

    ReplyDelete