Improved Spotify control

I have improved my control of Spotify by using Java JNA code to activate the Spotify client window before I send a stop/start request to it. This means that I do not need to know if Spotify is that current window before I send the stop or start request and I do not need to send a dummy playlist request. The same code can be the current artist and track so I can display this on my OpenRemote music control screen. I have also implemented a button to skip to the next track, and a button to shut down the music computer.

The JNA code is:


package net.geekgrandad;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.platform.win32.User32;

public class ActivateWindow {
  private static HWND wind;
  public static String title;
  
  public static HWND findWind(String name) {
	  final User32 user32 = User32.INSTANCE;
	  title=null;
	  wind = null;
	  user32.EnumWindows(new WNDENUMPROC() {
	      public boolean callback(HWND hWnd, Pointer arg1) {
	          char[] windowText = new char[512];
	          user32.GetWindowText(hWnd, windowText, 512);
	          String wText = Native.toString(windowText);
	          if (wText.startsWith(name)) {
	        	  title = wText;
	        	  wind = hWnd;
	        	  return false;
	          }
	          return true;
	      }
	  }, null);
	  return wind;
  }

  public static String activate(String app) {
    User32 user32 = User32.INSTANCE;
    HWND hWnd = findWind(app);
    user32.ShowWindow(hWnd, User32.SW_SHOWMAXIMIZED);
    user32.SetForegroundWindow(hWnd);
    return title;
  }
  
  public static String getTitle(String app) {
    findWind(app);
    return title;
  }
  
  public static void main(String[] args) {
	  System.out.println(activate("Spotify"));
  }
}
Advertisements
This entry was posted in Home automation and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s