Chaptr
Holding image

Why We Use Sonos and How We Live-Stream It

Like any other creative workspace, music plays a huge part of studio life at Chaptr. It helps motivate, inspire and, at times, console us. We rarely go a day without Chaptr radio and operate a wholly democratic system of choosing what gets played.
In the early days music would be pumped out to the office by whoever had the Mac with the best speakers. This meant that most of the music we all listened to was decided by one person with the odd request being queued up at the discretion of that day’s master of ceremonies. This was fine for a while – for the majority of the audience being introduced to new music a lot of the time was fun and gone was the continuous distraction of what to listen to next.
Then the team grew. We started employing people with, shall we say, passionate musical preferences. The free-jazz guys didn’t take to the partialities of the Nintendocore crowd and so on. Something had to change. We got Sonos.

The free-jazz guys didn’t take to the partialities of the Nintendocore crowd and so on.

Sonos is the most wondrous solution to the politics that come with communal music enjoyment. It’s basically a computer inside a really really good speaker that connects to a Wifi network. This means that a central music player can be controlled by any device that is connected to that network and capable of running the Sonos app. In short – everyone could finally participate in the choice of music played in the studio anonymously and directly. And so was born the democratisation of the Chaptr music selection.
It wasn’t a totally smooth ride. It took a few weeks for everybody to adjust to a new kind of etiquette that comes with a central music player controlled by multiple clients. The temptation to skip somebody’s guilty, or ‘should-be guilty’, pleasure or bury it at the back of the queue is easily enjoyed and hard to resist and we soon learnt that democracies don’t run themselves and the threat of totalitarian coups are real and persistent. We established some cardinal rules to protect the administration of “President Sonos” and collective shame was met by those who broke them. This system is still working peacefully to this day.
Now onto how we managed to get the current playing track on the office’s Sonos and share it with the world on our studio page. We’d seen a lot of fellow agencies sharing their weekly Spotify playlists with the world and we loved the idea of giving visitors to our website a taste of studio music culture – the only problem was that we really didn’t like the look of the native embedded Spotify player muddying our beautiful monochrome website. I decided to do some research and see if I could use Spotify’s API to display the track that was currently playing in the studio in a fully customised widget. Here’s how it went.

Spotify API

Sonos is just a computer and a speaker, the app uses third party music streaming services to stream the actual music and we tend to use one Spotify Premium account to play all our studio music. By this logic, I thought I could just tap into Spotify’s API by creating an app under the account associated with the service connected to Spotify and get the data of the currently playing track. Simple right? Nope.
Spotify doesn’t actually give you any information about what a user is currently listening to. This seemed odd to me as the Spotify API is rich with data and you’d think this would be a pretty basic request. Further to this, by simply authenticating a user’s individual account I wouldn’t have been able to be sure that the currently playing track was what the studio was listening to. What if Mitch (the owner of the all-powerful Spotify Premium account) was on holiday or listening to something down-right weird after office hours? This approach seemed messy regardless of its technical possibility. Back to the drawing board.

The Solution

Once I’d realised that Spotify’s API wasn’t going to help us out of the box and that this would have led to a pretty middle-man solution I started thinking differently about what it actually was we wanted to display. I realised that the data we were after wasn’t really associated with any individual’s Spotify account and that a much neater solution would be to get information from the Sonos computer itself. I looked into Sonos’ API but it was more concerned with controlling Sonos systems rather than getting information about what was happening on individual speakers. Also it was really bloody complicated. 2 days in and I didn’t have a solution for my self-initiated vanity project. I figured I had better stuff to be doing and decided to give up and think about settling for a more conventional embedded playlist. I forgot about the idea for a few hours until a colleague found the golden ticket by digging around in the Sonos preferences panel.

“Look you can scrobble to Last.fm!”

This was it – the missing component that would allow us to get information about what Sonos was currently playing and not the activity of one individual account that happened to be being played through the studio’s speakers. So here’s what we did:

  • Set up a Chaptr Last.fm account
  • Authenticate scrobbling to that account by the studio’s Sonos
  • Use the Last.fm API to get the last scrobbled track
  • Perform a lookup on that track’s artist and name to Spotify’s database of tracks
  • Retrieve the returned Spotify track ID and object
  • Display that track information on the website

Sure, the solution was a bit more convoluted than I originally anticipated but it worked and it worked well. It also meant that it would work regardless of what music service was streaming the studio’s music and wouldn’t rely on any one personal Spotify account so we can be sure that the track shown on the site wasa actually played in the Chaptr studio.
I’ve since done some research and found that this approach is discussed on forums and StackOverflow so I can’t take credit for an original approach. I even found an open source GitHub repository offering a downloadable solution however it uses a deprecated version of Spotify’s API to retrieve the track information and isn’t workable. We plan to release our own code to the wider development community soon.