Friday, January 5, 2018

Configuring Google Home to play music with iTunes

Welcome


This post explains how I configured a Google Home (GH) to play music through iTunes on Windows.  This makes the GH handle requests like "Hey Google, play Pentatonix Christmas" by playing the closest appropriate material in iTunes over our airplay speaker system (it doesn't play the music on the GH itself).  Here's a video of the system working:



Motivation


My family is heavily ingrained in Airplay, Apples proprietary system used to play to and sync many different speakers / audio systems.  We've got several Airplay speakers, and several Apple Airport Express's that are plugged into some old speaker systems that my Dad built.  On top of that, I've got every album our family has ever bought loaded onto iTunes on the home computer (running Windows 7), and that computer perpetually plays music throughout the house via Airplay.  Anyone in the family can jump on the iOS remote App to play music / adjust volume as desired.  The system works great (as long no one is using the microwave), and has for years.

Over Christmas, we got a new Google Home (thanks Fritz!).  If you ask the GH to play music, it will try to play internet radio through itself (it's a tiny speaker too).  After doing this once, it was immediately obvious to my folks and I that the Google Home should play through our iTunes & Airplay setup, and not through itself.  I found no way to do this correctly, so I hacked together a working solution.

Application Pipeline


The most time consuming process was determining exactly how to get from point A (GH verbal cue) to point B (play respective material through iTunes).  My original goal was have the GH talk directly to the iTunes computer over LAN.  I couldn't find a way to do that, so my goal shifted to minimizing the number of services the data would need to move through in order to get the job done.  This is the pipeline I arrived at:


IFTTT

IFTTT is a popular glue application.  It allows you to glue different apps / services you already have together.  Google Assistant supports IFTTT, so it's the first step in our process.  We'll setup several IFTTT applets to steal the GH commands play, play X, loudersofter, next, etc, and send them where we want them.

Push Bullet

IFTTT is great, but the only 3rd party support they offer is through WebHooks.  I didn't want our home computer to be facing the internet, so the IFTTT actions are all PushBullet notifications.  PushBullet offers an API where you can connect and fetch the latest notifications (or if you keep the connection open, they can be pushed directly to you).

My Own Application

This application connects to a PushBullet server, waits for a new notification, parses it, uses a bit of NLP to figure out what to play with iTunes, and plays the material in iTunes.  The application is written in C#, configured to boot on machine startup in the background, and communicates with iTunes using the very dated iTunes COM SDK.  The application is able to fetch all song / album / artist data from iTunes directly using the COM SDK, however there is no clean way that I'm aware of with the SDK to "play songs by Third Eye Blind."  The workaround is a designated playlist, called Automated, that we fill with songs that match our query and then play.

Follow my Footsteps


Start by making accounts for your Google Home, for IFTTT and for PushBullet.  Open the Google Home application and make sure your device is setup and on the network.  Now, open the IFTTT website, and we'll setup several different IFTTT Applets.  You'll need to sign in to Google Assistant and PushBullet within the IFTTT App.

IFTTT

Here are my IFTTT Applets.  Note the inclusion of "please" in front of reserved phrases like "play" and "pause".  Google typically doesn't let you override the standard home commands, but if you include the keyword "please", the API will let you override them, and in practice, you don't need to say please:


All of these use the Say a simple phrase (Google Assistant) trigger and the Push a note (Push Bullet) action.  The triggers can be whatever you want them to be, but if you're trying to use the software I've developed the actions have to identical to what I have.  The last Applet takes in custom phrases with the Say a phrase with a text ingredient trigger.  It uses the same Push a note action as before, but this time it includes the ingredient as a message for the notification.



You can test these without anything else setup, and if you've done everything correctly, the GH should respond with the correct reply.

Push Bullet

Log on to the Push Bullet website, go to settings, and click Create Access Token.  PushBullet will show a long key that corresponds to your account.  Copy it (or save it to a text file).  You'll need to enter it into my custom C# Application.

My Custom Application

Here is the installer for the most recent Windows Application.

Here is source code for the Windows Application. 

The iTunes COM SDK should be installed when you installed iTunes.  If you're having difficulties, make sure that iTunes is installed in the default directory.  I don't know how long apple will continue to support the iTunes COM SDK, but it at least works with iTunes 12.7.2.60 on Windows 7, 8, & 10.

When you run my custom application for the first time, you'll have to copy the PushBullet access token from into the GUI.  It should remember it in the future.  I've gone ahead and set the application to run on startup on our machine.  If it has a valid PushBullet secret, it will minimize itself shortly after launch to the taskbar (where you can only exit by right clicking and selecting exit).

That's it!


Good luck!


EDIT 1:

Push bullet accounts periodically expire (around every 120 days).  If the system stops working, try logging into PushBullet on your browser and trying again.