Album highlight:Since their inception in late 2006, Tuesday Spoils have been catching eyes and ears with their unique, vibrant sound and enigmatic live shows. They have recently released their new EP as a free download along with an iTunes LP. |
Monitoring tracks and track changesWhen iTunes is playing a track you can check what is playing by looking at the window.iTunes.currentPlayingTrack object. This object always contains a reference to the currently playing track and holds several properties and methods that you can use. This track object is exactly the same as the ones described in the Controlling iTunes tutorial, which means that you can tell it to play() and you can get some basic information about the track; it's name, album and artist. Normally you know what the current playing track is, because you've explicitly told iTunes to play that particular track. However when playing a playlist it would be nice to be able to know when a track change has occurred so that you can for example automatically change to the correct page with track info. First we start by taking our example from the Controlling iTunes tutorial which we are going to expand a bit later on:
var albumTracks = null;
var albumPlaylist = null;
Array.prototype.sortOn = function(aField) {
var ArraySortField = "";
function fieldSort(a, b) {
if (a[ArraySortField] > b[ArraySortField])
return 1;
if (a[ArraySortField] < b[ArraySortField])
return -1;
return 0;
}
ArraySortField = aField;
this.sort(fieldSort)
}
function playTrack(aTrackNumber, aFromPlaylist) {
// If we want to play the track from the playlist (ie. continue with the rest of the songs after this one has finished playing), we use the playlist's play function
if (aFromPlaylist) {
albumPlaylist.play(aTrackNumber);
} else {
// Otherwise just play a single track
var theTrack = albumTracks[aTrackNumber];
if (theTrack) theTrack.play();
}
}
function findAlbumTracks(aAlbumName) {
var theTracks = window.iTunes.findTracksByTextFields({album: aAlbumName});
var theTrackList = [];
if (theTracks != null)
for (var i = 0; i < theTracks.length; i++) {
var theTrack = theTracks[i];
if ((theTrack != null) && (theTrack.name.indexOf("iTunes LP") == -1))
theTrackList.push(theTrack);
}
theTrackList.sortOn("trackNumber");
theTrackList.sortOn("discNumber");
albumPlaylist = window.iTunes.createTempPlaylist();
albumTracks = theTrackList;
albumPlaylist.addTracks(albumTracks);
}
findAlbumTracks("iTunes LP");
This searches through the iTunes Music library for all tracks that are from the requested album, iTunes LP. You can change this last liine to make the code search through your library for any album you want. The playTrack function behaves differently if you want it to play a track from a playlist or just a single track. When we're playing from a playlist we want to start monitoring track changes. To do that we modify the playTrack function to start a timer loop which checks the track at regular intervals:
var changeFunction = null;
var changeTimer = -1;
var lastTrack = null;
function playTrack(aTrackNumber, aFromPlaylist) {
// If we want to play the track from the playlist (ie. continue with the rest of the songs after this one has finished playing), we use the playlist's play function
// Cancel any listeners that might still be active
stopListeningToTrackChanges();
if (aFromPlaylist) {
startListeningToTrackChanges();
albumPlaylist.play(aTrackNumber);
} else {
// Otherwise just play a single track
var theTrack = albumTracks[aTrackNumber];
if (theTrack) theTrack.play();
}
}
function startListeningToTrackChanges() {
if (changeFunction != null) {
// If there is a handler for the changes then start checking every 0.5 seconds
changeTimer = setInterval(checkForChanges, 500);
}
}
function stopListeningToTrackChanges() {
if (changeTimer != -1) clearInterval(changeTimer);
changeTimer = -1;
}
function checkForChanges() {
var theCurrentTrack = window.iTunes.currentPlayingTrack;
if (theCurrentTrack != lastTrack) {
lastTrack = theCurrentTrack;
if (theCurrentTrack)
for (var i = 0; i < albumTracks.length; i++) {
if ((theCurrentTrack.name == albumTracks[i].name) &&
(theCurrentTrack.trackNumber == albumTracks[i].trackNumber) &&
(theCurrentTrack.discNumber == albumTracks[i].discNumber)) {
changeFunction(i);
return;
}
}
changeFunction(-1);
stopListeningToTrackChanges();
}
}
Now as soon as you start playing a track with the playTrack function and you tell it to treat is as a track within a playlist by passing true as the second parameter; the function startListeningToTrackChanges will be called. This will start a timer loop that will check if the currentPlayingTrack is different than the one that was last registered. If so, notify the changeFunction of the change. If the currently playing track is null it means that the playlist has finished, and we stop listening. You'll see that changeFunction is now a var defined as null at the top of the code; and if you check the startListeningToTrackChanges function you'll see that if changeFunction is null it won't start the timer loop, because there is no code to handle track changes. So the last thing we have to do is create a function that can handle these track changes, and we need a place to display the changes. For this example of our new index.html I'm assuming you've placed all the above code in a single .js file called trackController.js:
<html>
<head>
<title>iTunesLP.net - Track controller example</title>
<script language="javascript" type="text/javascript" src="trackController.js"></script>
<script language="javascript" type="text/javascript">
function handleChanges(aTrackNumber) {
// If the tracknumber is -1 then the playlist has stopped playing, otherwise show the currently playing tracknumber (which is zero based).
var theValue = (aTrackNumber == -1) ? "No track playing" : ("Playing track " + (aTrackNumber+1));
document.getElementById("displayTrackNumber").value = theValue;
}
// Register the handleChanges function as the handler for track changes
changeFunction = handleChanges;
// And start playing a track from a playlist
playTrack(0, true);
</script>
</head>
<body>
<input type="text" id="displayTrackNumber" />
</body>
</html>
If you placed the earlier javascript code in a file called trackController.js and changed the album name to an album which exists in your iTunes music library, then placing this html in your iTunes LP should start playing the first track of that album and updating the input field on your page with the current track number. You can use that tracknumber to look up more track information in the albumtracks array. Using albumtracks[aTrackNumber].name for example will give you the name of the currently playing track. Be careful though; aTrackNumber might be -1 if the playlist has finished playing, and if so, albumTracks[aTrackNumber] will not be a valid object, and trying to request it's name property will give a javascript error. | |||