Asterisk, PHP, and iTunes?
What do these three things have anything in common? Independently, they are all extremely useful. Together, they produce an auto-play-pause-caller-id-phone-system.
The idea is this. I use iTunes to play music on my Mac. I listen to it all day long. The problem is that I get lots of phone calls and am always finding myself either pausing iTunes or more likely muting the computer. The issue arises when three hours has past since my last phone call and my computer's music is still muted. I miss all of my precious music.
The solution. Hook up iTunes to know when I answer, make and hang-up calls. Luckily, I use a Mac. OS X has php pre-installed and makes producing software easy. Also, Asterisk, the open source PBX upon which our phone system runs, has an awesome API. The API allows us to login to their system via a TCP socket and monitor all activity.
So how does it all tie together?
When my computer boots, I have it start a php script that connects to Asterisk and demands it send all call activity. PHP then monitors this activity and filters out only activity for my phone. When PHP detects that a phone call has been made, the information is passed to an AppleScript that triggers iTunes to pause and a dialog to appear stating "Outgoing call". This was made easy by use of Growl. I did the same thing for incoming calls except I show the caller id info for that. Once I hang up, iTunes is called to continue playing my playlist.
There were a few obstacles that needed to be overcome. When I was on the phone and someone else called in and hung up, my music began blasting again. The person I was on the phone with at the time was like "What is that?" I reprogrammed my script to use a stack to make sure it will only play music once all my calls were hung up. Another obstacle was getting the correct information to read. Although the Asterisk interface is extremely powerful, it is hard to get to know how to correctly monitor phone activity. The final stumbling block was getting iTunes to only respond if it was open. AppleScript does not have a "tell application if open" command. You'll see my workaround below.
Want some code? You are free to use the code below to do that you want. Just give me some credit as outlined in the copyright below.

1 OLDER COMMENT
posted by Barry Schwartz on: Jun 19, 2006 11:43am
I love this script, works wonders for me.
0 COMMENTS