Daily Archive for December 5th, 2005

iTunes COM – Part Two

Ok, now that you’ve got Apache2 and PHP5 running, we need to make the actual code. So, here is the first part:


try {
$iTunes = new COM('iTunes.Application');
} catch (Exception $comexc) {
// do nothing, caught exception.
}

That pretty much connects us to iTunes, and catches an exception if it doesnt work, for example if COM Calls are disabled in iTunes (some people do it for security measures). Whenever we want to reference iTunes, we work with the $iTunes variable.


$Plylst = $iTunes->CurrentPlaylist;
if (!isset($_GET["act"])) {
$currentTrack = $iTunes->CurrentTrack;
$trackName = $currentTrack->Name;
$trackNumber = $currentTrack->TrackNumber;
$albumName = $currentTrack->Album;
$artistName = $currentTrack->Artist;
$playlist = $iTunes->LibraryPlaylist->Tracks;
// code for the play song from location thing.
$location = array();
$name = array();
$num = $playlist->Count;
echo "Number of Tracks in Library: ", $num;
for ($i = 0; $i < $num; $i++) {
//while ($i < $num) {
$track = $playlist[$i + 1];
$trackadd = $iTunes->LibraryPlaylist->Tracks[$i + 1];
array_push($name, "$track->Name");
array_push($location, "$trackadd->Location");
}

That pretty much assigns variables to the current track, the time in the track, the time remaining, and the artist and so on. After that finishes, we create two different arrays, one that stores the location to the MP3, and another one that stores the name of the mp3 itself. This is so that you can create a select box with the names and locations of the mp3s, so you can allow people to control your itunes over the internet. More specifically, the $iTunes->Playfile method doesnt take an mp3 name (Blahblah.mp3), you require a full path name (C:\Documents and Settings\….\My Music\Blahblah.mp3). That is pretty much some of the stuff you need to create a PHP App that controls iTunes. There are many possibilities for an application like this, as I made a msgrp2p.xml that lets you control my iTunes through a MSN Messenger activity (with the new Messenger Activities API). Neat eh? Part three will feature the raw code to the msgrp2p.xml and the iTunes controller. Theyre both pretty messy but they do the job.

Enjoy.