Let me start off by saying that if I were just setting out to create a “whole house audio” system on the cheap with the mind to integrate it with Vera, I would most likely go with the Slingbox based solution that you will find information about elsewhere on the forums. From my research I have found that solution to be more flexible, more hackable, and have better sound quality than Itunes with Airtunes. That being said, I had already built my iTunes/Airtunes network including 5 airport expresses before discovering Vera, so from lemons I am making lemonade. For better of for worse, my setup actually works quite well.
I have a dedicated mac mini that I use to run an iTunes instance, as well as a Plex media player (connected to my TV) but this can also be used just as effectively on your regular mac based workstation (I started out on my office Mac pro). Note that this solution is MAC ONLY, sorry Windows iTunes users. Most of the logic used here is applescript, that runs on the mac hosting iTunes. The need for luup in scenes code is actually quite minimal but could be expanded to include more logic with someone with a greater imagination than I.
Below are the steps I followed to enable playlist based iTunes control, and well as basic start stop functionality to my Vera scenes. I have one scene called “Dinner Time” for example that turns down my living room lights, brings my dining room overhead dimmer up to 65% turns on some accent lights before connecting to iTunes and setting output to my kitchen and living room stereo speakers and playing a playlist called “Dinner Time” on a random shuffle. This sets the mood nicely for a nice meal with friends, and requires only pressing one button on my HA09 remote or VRCS4.
[ul][li]Step 1 - Setup your Mac’s (Apache) Web Server to control iTunes from web requests[/li][/ul]
This step involved making changes to core system config files as root, so be sure you know what you are doing before attempting. Please understand that the changes you are making will allow people with http/https access to your machine to execute commands AS YOUR LOCAL USER, so be extremely careful with how this is setup. I run iTunes as a dedicated user, on a machine that only hosts my media, and is not in anyway accessible from the outside network to keep my risk low, even then I understand that this strategy carries an inherent security risk.
There is a handy guide on how to accomplish this here: [url=http://www.whatsmyip.org/itunesremote/]http://www.whatsmyip.org/itunesremote/[/url].
Once this is done you can control iTunes from any web browser by entering the appropriate URL - nice.
[ul][li]Step 2 - Modify iTunes control script to include AirTunes integration, and playlist control[/li][/ul]
If you followed the guide listed in Step 1 - you will already have the ability to control basic functions (start/stop/next/prev/etc) of your iTunes from a URL. If this is all you want, skip to step 4. Most of us however will want to get a little fancier with this setup to be able to do the things I described above with my Dinner Time solution. This will require modifications to the PHP file which was called “control.php” in the guide, as well as some custom applescript as defined in Step 3.
Here is Mine with the dinner time example:
<html>
<head>
</head>
</body>
<?
$q = $_GET['q'];
switch ($q)
{
case "":
echo "You need to send me a command, then I shall execute it";
break;
case "play";
exec("osascript -e 'tell app \"iTunes\" to play'");
echo "Playing";
break;
case "pause";
exec("osascript -e 'tell app \"iTunes\" to pause'");
echo "Pausing";
break;
case "playpause";
exec("osascript -e 'tell app \"iTunes\" to playpause'");
echo "Toggling Play";
break;
case "next";
exec("osascript -e 'tell app \"iTunes\" to next track'");
echo "Next Track";
break;
case "prev";
exec("osascript -e 'tell app \"iTunes\" to previous track'");
echo "Previous Track";
break;
case "louder";
exec("osascript -e 'tell app \"iTunes\" to set sound volume to sound volume + 5'");
echo "Turning Up the Volume";
break;
case "quieter";
exec("osascript -e 'tell app \"iTunes\" to set sound volume to sound volume - 5'");
echo "Turning Down the Volume";
break;
case "mute";
mutev();
echo "Muting the Volume";
break;
case "script_dinner";
exec("osascript /[SCRIPT PATH]/script_dinner.scpt");
echo "Executing Dinner Time Script";
break;
}
function mutev()
{
echo "start mute function<br>";
$data = file_get_contents("/Library/WebServer/Documents/volume.txt");
$logfile = fopen("/Library/WebServer/Documents/volume.txt",'w');
$oldvolume = exec("osascript -e 'tell app \"iTunes\" to sound volume'");
echo "volume data:$data:<br>";
if ($data == "x")
{
fwrite($logfile,$oldvolume);
exec("osascript -e 'tell app \"iTunes\" to set sound volume to 0'");
}
else
{
fwrite($logfile,"x");
exec("osascript -e 'tell app \"iTunes\" to set sound volume to $data'");
}
fclose($logfile);
}
?>
[ul][li]Step 3 - Creating the Applescript to handle playlists/Airtunes control. [/li][/ul]
This could be done in several ways. For the sake of simplicity I have a script for each “scene” that will involve complex configuration. Each of which are called by the PHP commands as you have already seen (see script_dinner example above). I will be the first to admit that I am not an Applescript expert. Also, Apple have deliberately or otherwise made it quite difficult to control Airtunes from script. The method you see here works, but definitely falls under the “hack” category. As of now, I am navigating the Airtunes speaker menu and multiple speaker popup window by element number so there is a potential instability here that if one or more of your airport expresses is off the network, thus changing the number if items in each list, it may break. I have good reason to believe that it is possible to do this navigation explicitly by name, but have not sorted out how. (feel free to fix this and report back) ![]()
You will need to enable the “Enable access for assistive devices” in your iTunes system’s Universal Access preference pane for this to work. This setting allows scripts to interact with the system by emulating keyboard and mouse input.
Here is my dinner time applescript as referenced by the above PHP command ‘script_dinner’.
EDIT: See [url=http://fall-line.com/2010/09/control-airplayairtunes-feature-of-itunes-with-applescript/]http://fall-line.com/2010/09/control-airplayairtunes-feature-of-itunes-with-applescript/[/url] or the below post in this thread for iTunes 10 version
-- Remember to turn on access for assistive devices.
-- Working as of July 2010 with iTunes 9.2
activate application "iTunes"
tell application "System Events"
tell application process "iTunes"
if (name of button 16 of window "iTunes" as text) ≠ "" then
click button 16 of window "iTunes" --Press the iTunes Speaker Button
delay 0.01
-- Traverse down the list of speakears (up to 7) -- If you have MORE than 6 airport Express units, make this a larger number to accomodate.
repeat with i from 1 to 7
key code 125
end repeat
keystroke return
repeat with i from 1 to (count of every UI element of window 1)
tell UI element i of window "Multiple Speakers"
set rowcount to count of rows
if rowcount > 0 then -- Do we have Speakers available?
-- Note that if your list of speakers changes at any point (due to an airport express being powered off, etc) the selection by row # will break.
-- If you rename them, changing their alphabetical order, you will be turning on the wrong speaker.
-- This logic needs to be fixed by someone who knows iTunes scripting better than I do.
-- Turn on the Kitchen Speaker (row 2) if it is off
if (value of checkbox 1 of row 2) as integer = 0 then
click checkbox 1 of row 2
end if
-- Turn on Living Room Speaker (row 1) also, if it is off.
if (value of checkbox 1 of row 1) as integer = 0 then
click checkbox 1 of row 1
end if
-- Now that we have enabled another speaker (satisfying iTunes), we can shut off the computer speakers
if (value of checkbox 1 of row 5) as integer = 1 then
click checkbox 1 of row 5
end if
end if
end tell
end repeat
end if
end tell
end tell
-- Finally, play the Dinner Time playlist. All you need to do here is create a playlist called "Dinner Time", etc.
tell application "iTunes"
set shuffle of playlist "Dinner Time" to 0
set shuffle of playlist "Dinner Time" to 1
play playlist "Dinner Time"
end tell
[ul][li]Step 4 - Include Luup URL calls in the appropriate Vera scenes to have them execute your iTunes commands when executing[/li][/ul]
From my “Dinner Time” Vera Scene’s Luup code:
[code]luup.inet.wget(“http://[ITUNES HOST]/control.php?q=script_dinner”)
Replace [ITUNES HOST] with your IP address or hostname
[/code]
From my “Good Night” scene that turns off all lights and stops iTunes if it is playing:
[code]luup.inet.wget(“http://[ITUNES HOST]/control.php?q=pause”)
Replace [ITUNES HOST] with your IP address or hostname
[/code]
[ul][li]Step 5 - Going beyond[/li][/ul]
One of the limitations of using iTunes as a whole house adio system with our without vera integration is with the airport express outputs. Most users simply connect them to an existing stereo etc since they do not themselves provide amplified speaker connections. This means you have to go turn on the stereo in each room and set the appropriate input in most cases - not ideal for the one touch integration we are looking for. You can remedy this problem by either integrating your stereo into Vera as well (via IR, RS-232, etc) and/or by including auto-on amplifiers/speakers with your Airport express such as these:
[ul][li]ZVOX Mini[/li]
[li]Audioengine A5[/li]
[li]AudioSource AMP-100[/li][/ul]
(I use this with some Polk bookshelf speakers)
Each of the above options will sit in a low power ‘sleep’ mode until such time as there is a signal detected on the audio in port, and then will power up and start playing your tunes.
If your iTunes machine is not always on, I would also recommend enabling the “Wake for Ethernet network access” (WOL) feature in your system’s power saver settings so that it will wake from sleep when you attempt to control it.
Other ideas:
[ul][li]Play a specific playlist when a code is used to unlock your Schlage door lock, welcoming you home[/li]
[li]Play & repeat a specific track over all speakers as an alarm for certain conditions[/li]
[li]Subtly dim the lights and put on a nighttime soundtrack in a kids room before bedtime[/li]
[li]You tell me… [/li][/ul]
I hope this helps a few of you to reproduce the setup I have here, or provides you the information you need to customize it to your own desires, and improve on it! It certainly isn’t perfect, but I must admit it is pretty slick at times. I’d be happy to try and answer any questions… but everybody’s system is different. Like I mentioned above this is probably a solution best left to those who have some experience with scripting and who are comfortable working in the terminal of their Macs.
Cheers