Playing Spotify playlists from OpenRemote

I mainly use Spotify to play music, and I have lots of playlists. I have been using OpenRemote to play the Spotify playlists for some time, but a while ago an incompatible change in the treatment of Spotify URLs stopped it from working. I have just got it working again (after a fashion).

I have an OpenRemote screen with a button for each of my most used playlists. When I press the button a “play <playlist>” TCP/IP command is sent to the ORControl Java program running locally on the OpenRemote server machine. This converts the playlist name to a Spotify HTTP Link URL using a HashMap. I then send a “play url” command to my music server computer.

I used to send the command directly from OpenRemote to the music server, but that causes a lot of errors when the music server computer is not running. I can now trap any errors and monitor the status of the music server computer in ORControl.

On the music server computer, I run the ORMusicServer java program. This receives requests by a socket in a way that is compatible with an OpenRemote TCP/IP command.

When the music computer receives a play command it does:

java.awt.Desktop.getDesktop().browse(java.net.URI.create(cmd.substring(5)));

This opens a browser window shows the playlist, starts Spotify if necessary, and shows the playlist in Spotify. It used to play the play list, but this changed in December last year. The current behaviour may be browser dependant, but in Chrome is starts the playlist if nothing is currently playing.

This is OK for starting a playlist when nothing is playing, but is no use to change the current playlist.

Using the browser in this way is not very nice. A new tab is opened each time a playlist request is sent. (Again this may be browser dependant). I tried sending a Spotify URI instead of an HTTP URL, but I cannot find a way to make this work from Java. Spotify does not seem to have an API for doing this. Its API is for running applications within Spotify, not for controlling Spotify from outside. If anyone knows of a better way to do this, please let me know. It does not have to be in Java – any language that supports socket servers is OK.

Anyway, a slight improvement on this is that there is an undocumented parameter “autoplay=true” that can be added to Spotify HTTP Link URLs. Unfortunately, this does not force an autoplay of the playlist as it implies, but instead causes the Spotify window to be brought to the forefront and given the focus. This is useful, so I now add this parameter to the URL.

However, this does not solve the problem of playing the playlist when another is currently playing.

To solve this problem, I decided to use the java.awt.Robot class to send key presses to Spotify. When Spotify has the focus, a space key stops or starts the current song. So I added a Stop button to the music screen on my OpenRemote panel. It sends a space key press and then a release.

With this in place, I can stop and start playlists from OpenRemote again. It also adds the extra, useful facility to pause and restart a song, as the same (misnamed) Stop button does this.

To start a playlist when Spotify is not playing, I just press the playlist button, and after a few seconds, it starts playing.

To start a playlist when one is already playing, I need to make sure Spotify has the focus. If I don’t know if it currently has the focus, I can re-press the playlist button for the currently playlist playlist. This gives it the focus, but does not stop or restart the playlist. I then press the Stop button, and a space keypress is sent to stop the current playlist. I then press the button for the new playlist and it starts!

Again, if anyone knows of a better way of doing this, please let me know.

Media players like XBMC which have an http interface, are much much easier to control, but do not give me access to the huge catalogue of music that Spotify does.

I currently use Spotify Unlimited which costs me five pounds a month, rather than Spotify Premium which would cost me ten pounds. This means that I do not have access to the mobile Spotify clients, including the ARM clients. So I cannot run the open source Despotify client, and I cannot run Spotify on the Raspberry Pi.

Advertisements
This entry was posted in Electronics, Home automation. 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