Building a Spotify App
On Wednesday November 30, Spotify announced their Spotify Apps platform that will let developers create Spotify-powered music apps that run inside the Spotify App. I like Spotify and I like writing music apps so I thought I would spend a little time kicking the tires and write about my experience.
First thing, the Spotify Apps are not part of the official Spotify client, so you need to get the Spotify Apps Preview Version. This version works just like the version of Spotify except that it includes an APPS section in the left-hand navigator.
If you click on the App Finder you are presented with a dozen or so Spotify Apps including Last.fm, Rolling Stones, We are Hunted and Pitchfork. It is worth your time to install a few of these apps and see how they work in Spotify to get a feel for the platform. MoodAgent has a particularly slick looking interface:
A Spotify App is essentially a web app run inside a sandboxed web browser within Spotify. However, unlike a web app you can't just create a Spotify App, post it on a website and release it to the world. Spotify is taking a cue from Apple and is creating a walled-garden for their apps. When you create an app, you submit it to Spotify and if they approve of it, they will host it and it will magically appear in the APPs sections on millions of Spotify desktops.
To get started you need to have your Spotify account enabled as a 'developer'. To do this you have to email your credentials to platformsupport@spotify.com. I was lucky, it took just a few hours for my status to be upgraded to developer (currently it is taking one to three days for developers to get approved). Once you are approved as a developer, the Spotify client automagically gets a new 'Develop' menu that gives you access to the Inspector, shows you the level of HTML5 support and lets you easily reload your app:
Under the hood, Spotify Apps is based on Chromium so those that are familiar with Chrome and Safari will feel right at home debugging apps. Right click on your app and you can bring up the familiar Inspector to get under the hood of your application.
Developing a Spotify App is just like developing a modern HTML5 app. You have a rich toolkit: CSS, HTML and Javascript. You can use jQuery, you can use the many Javascript libraries. Your app can connect to 3rd party web services like The Echo Nest. The Spotify Apps supports just about everything your Chrome browser supports with some exceptions: no web audio, no video, no geolocation and no Flash (thank god).
Since your Spotify App is not being served over the web, you have to do a bit of packaging to make it available to Spotify during development. To do this you create a single directory for your app. This directory should have at least the index.html file for your app and a manifest.json file that contains info about your app. The manifest has basic info about your app. Here's the manifest for my app:
% more manifest.json
{
"BundleType": "Application",
"AppIcon": {
"36x18": "MusicMaze.png"
},
"AppName": {
"en": "MusicMaze"
},
"SupportedLanguages": [
"en"
],
"RequiredPermissions": [
"http://*.echonest.com"
]
}
The most important bit is probably the 'RequiredPermissions' field. This contains a list of hosts that my app will communicate with. The Spotify App sandbox will only let your app talk to hosts that you've explicitly listed in this field. This is presumably to prevent a rogue Spotify App using the millions of Spotify desktops as a botnet. There are lots of other optional fields in the manifest. All the details are on the Spotify Apps integration guidelines page.
I thought it would be pretty easy to port my MusicMaze to Spotify. And it turned out it really was. I just had to toss the HTML, CSS and Javascript into the application directory, create the manifest, and remove lots of code. Since the Spotify App version runs inside Spotify, my app doesn't have to worry about displaying album art, showing the currently playing song, album and artist name, managing a play queue, support searching for an artist. Spotify will do all that for me. That let me remove quite a bit of code.
Integrating with Spotify is quite simple (at least for the functionality I needed for the Music Maze). To get the currently playing artist I used this code snippet:
var sp = getSpotifyApi(1);
function getCurrentArtist() {
var playerTrackInfo = sp.trackPlayer.getNowPlayingTrack();
if (playerTrackInfo == null) {
return null;
} else {
return track.album.artist;
}
}
To play a track in spotify given its Spotify URI use the snippet:
function playTrack(uri) {
sp.trackPlayer.playTrackFromUri(uri, {
onSuccess: function() { console.log("success");} ,
onFailure: function () { console.log("failure");},
onComplete: function () { console.log("complete"); }
});
}
You can easily add event listeners to the player so you are notified when a track starts and stops playing. Here's my code snippet that will create a new music maze whenever the user plays a new song.
function addAudioListener() {
sp.trackPlayer.addEventListener("playerStateChanged", function (event) {
if (event.data.curtrack) {
if (getCurrentTrack() != curTrackID) {
var curArtist = getCurrentArtist();
if (curArtist != null) {
newTree(curArtist);
}
}
}
});
}
Spotify cares what your app looks like. They want apps that run in Spotify to feel like they are part of Spotify. They have a set of UI guidelines that describe how to design your app so it fits in well with the Spotify universe. They also helpfully supply a number of CSS themes.
Getting the app up and running and playing music in Spotify was really easy. It took 10 minutes from when I received my developer enabled account until I had a simple Echo Nest playlisting app running in Spotify
Getting the full Music Maze up and running took a little longer, mainly because I had to remove so much code to make it work. The Maze itself works really well in Spotify. There's quite a bit of stuff that happens when you click on an artist node. First, an Echo Nest artist similarity call is made to find the next set of artists for the graph. When those results arrive, the animation for the expanding the graph starts. At the same time another call to Echo Nest is made to get an artist playlist for the currently selected artist. This returns a list of 'good' songs by that artist. The first one is sent off to Spotify for it to play. Click on a node again and the next song it that list of good songs plays. Despite all these web calls, there's no perceptible delay from the time you click on a node in the graph until you hear the music.
Here's a video of the Music Maze in action. Apologies for the crappy audio, it was recorded from my laptop speakers.
[youtube http://youtu.be/finObm36V6Y]
Here's the down side of creating a Spotify App. I can't show it to you right now. I have to submit it to Spotify and only when and if the approve it, and when they decided to release it will it appear in Spotify. One of the great things about web development is that you can have an idea on Friday night and spend a weekend writing some code and releasing it to the world by Sunday night. In the Spotify Apps world, the nimbleness of the web is lost .
Overall, the developer experience for writing Spotify Apps is great. It is a very familiar programming environment for anyone who's made a modern web app. The debugging tools are the same ones you use for building web apps. You can use all your favorite libraries and toolkits and analytics packages that you are used to. I did notice some issues with some of the developer docs - in the tutorial sample code the 'manifest.json' is curiously misspelled 'maifest.json'. The JS docs didn't always seem to match reality. For instance, as far as I can tell, there's no docs for the main Spotify object or the trackPlayer. To find out how to do things, I gave up on the JS docs and instead just dove into some of the other apps to see how they did stuff . (I love a world where you ship the source when you ship your app). Update - In the comments Matthias Buchetics points us to this Stack Overflow post that points out where to find the Spotify JavaScript source in the Spotify Bundle. At least we can look at the code until the time when Spotify releases better docs.
Update2 - here's a gist that shows the simplest Spotify App that calls the Echo Nest API. It creates a list of tracks that are similar to the currently playing artist. Another Echo Nest based Spotify App called Mood Knobs is also on github.
From a technical perspective, Spotify has done a good job of making it easy for developers to write apps that can tap into the millions of songs in the Spotiverse. For music app developers, the content and audience that Spotify brings to the table will be hard to ignore. Still there are some questions about the Spotify Apps program that we don't know the answer to:
How quickly will they turn around an app? How long will it take for Spotify to approve a submitted app? Will it be hours, days, weeks? How will updates be managed? Typical web development turnaround on a bug fix is measured in seconds or minutes not days or weeks. If I build a Music Hack Day hack in Spotify, will I be the only one able to use it?
How liberal will Spotify be about approving apps? Will they approve a wide range of indie apps or will the Spotify App store be dominated by the big music brands?
How will developers make money? Spotify says that there's no way for developers to make money building Spotify apps. No Ads, no revenue share. No 99 cent downloads. It is hard to imagine why developers would flock to a platform if there's no possibility of making money.
I hope to try to answer some of these questions. I have a bit of cleanup to do on my app, but hopefully sometime this weekend, I'll submit it to Spotify to see how the app approval process works. I'll be sure to write more about my experiences as I work through the process.