Saturday 27 November 2010

Control Spotify from XBMC with Mopidy

Update: How well does this work - not very good (with XBMC). Seems that the client in XBMC pretty much doesn't work with Mopidy - sadly. Information available here.

What to Expect from this:

  • If you add and play songs from gmpc, they will play and be visible playing in XBMC
  • The queue in XBMC will not show any songs, except the one playing. But items added to queue in XBMC will actually be added
  • If you control Mopidy from XBMC the song will switch every 5 seconds or so.
  • Using the XBMC client may cause Mopidy to crash - at least it did so for me.
  • Controlling from another client, such as gmpc works fine though, so you can still have a Spotify MPD server =)
I guess most people out there already deleted their MP3-collection and use Spotify or some other means of streaming music. The bad thing is that you really can't use XBMC with such solutions. There was/is some project that tried to implement native Spotify in XBMC, it looks promising and I hope they make it usable. In the mean time, this is my solution to listen to Spotify from XBMC. Note that this requires Spotify premium.


Mopidy

Mopidy is a python MPD server which can use Spotify as a backend. If you don't know what an MPD server is, it basically a media player with many remotes. By that I mean that the audio will stream out from one machine (usually to speakers) but that you can control it from multiple other machines, or locally.


I use Ubuntu Desktop 10.10 32-bit for this install. Basically we follow the install documentation found in Mopidy's documentation.

Install required packages

sudo aptitude install git-core python-alsaaudio python-dev build-essential python-pip

Install libspotify

Note that the 0.0.6 version of libspotify will not work with pyspotify, so for now we need the 0.0.4 version.


32-bit
wget http://developer.spotify.com/download/libspotify/libspotify-0.0.4-linux6-i686.tar.gz
tar zxfv libspotify-0.0.4-linux6-i686.tar.gz
cd libspotify-0.0.4-linux6-i686/
sudo make install prefix=/usr/local
sudo ldconfig

64-bit
wget http://developer.spotify.com/download/libspotify/libspotify-0.0.4-linux6-x86_64.tar.gz
tar zxfv libspotify-0.0.4-linux6-x86_64.tar.gz
cd libspotify-0.0.4-linux6-x86_64/
sudo make install prefix=/usr/local
sudo ldconfig


Install pyspotify
The instructions available at the official website, but are repeated for completeness:

git clone git://github.com/jodal/pyspotify.git
cd pyspotify/pyspotify/
sudo rm -rf build/               # If you are upgrading pyspotify
sudo python setup.py install

Installing Mopidy

sudo pip install Mopidy

Configure Mopidy

Create the following file in ~/.mopidy/settings.py, and place the following contents in it (yes you should replace myusername and mysecret, but the u-at the start should be there)
SPOTIFY_USERNAME = u'myusername'
SPOTIFY_PASSWORD = u'mysecret'

Running Mopidy

Run the command Mopidy, it should report if it can connect to Spotify like this:
zerqent@blitz:~$ mopidy
INFO     -- Starting Mopidy --
** Message: pygobject_register_sinkfunc is deprecated (GstObject)
INFO     Mopidy uses SPOTIFY(R) CORE
INFO     Disabled: mopidy.frontends.lastfm.LastfmFrontend (No module named pylast)
INFO     MPD server running at [::ffff:127.0.0.1]:6600
INFO     Connected to Spotify

MPD Client in Gnome

We need an MPD client to talk to Mopidy, you can look for compatible clients here. In Gnome you can for instance use GMPC.

sudo aptitude install gmpc

Just hit Connect then Forward
Setup of gpmc is pretty simple, just press forward once, then "Connect" and then Forward and Finish. You will probably find this interface somewhat limited compared to Spotify but it does work. You can get your Spotify playlists, you can search, but the browsers might just give you artists that are in one of your playlists or just hang it seems.

GMPC with Mopidy
MPD Client in XBMC
Note that I am assuing that you are using one of the Dharma (e.g. beta/release candidates right now) versions of XBMC, if not you will have to google for an MPD Client.

Navigate to System->Add-Ons->Get Add-Ons -> XBMC.org Add-Ons -> Music Addons and install MPD Client.

From the XBMC root-menu you can reach MPD Client under Music->Add-Ons.




There is really no configuration needed to get the MPD Client to work with Mopidy, since it defaults to the correct settings. So just pick one of the profiles and you should be connected.


Now I haven't used this Client to much so I am not sure how good it is - but it might be a viable solution to trying to control Spotify with vnc or similar.

Remote control of Mopidy

Now one of the reasons you want an MPD-server for serving Music instead of a local music program is that you can remote control it - yes I know this is fully possible with the built-in XBMC web interface, but that doesn't have Spotify-support (yet?).

To allow Mopidy to be controlled from other network units edit ~/.mopidy/settings.py and add:
MPD_SERVER_HOSTNAME = u'0.0.0.0'
Note that this means that everyone can connect to Mopidy, unless you have something else, like a NAT/Firewall, that blocks them. You can limit this more, for that RTFM.