Subscribe to RSS
Download
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.
 

Playing background audio

Most iTunes LPs start with some kind of background audio playing. Some have that audio playing in a loop whenever no other track is playing. Such an audioloop is automatically played by TuneKit when in the controllers/data.js the audioloop property is set to a valid audio location. However since we won't be using TuneKit we will do it slightly different.

To play back background audio iTunes LPs use the HTML Audio object. You will usually want to start playing the intro and background audio after the full html has finished loading. In our example we will wait for the page to finish loading, and then after a delay of 0.5 second we will start the audio and after another 0.5 second we will start the intro playback. First we handle the loading of the page:

<body onload="init()">

Then in that init function set up the delays for the audio playback and starting the intro:

function init() {
	setTimeout(startAudio, 500);
	setTimeout(startIntro, 1000);
}

And finally playing back the audio using the Audio object is as ease as:

function startAudio() {
	var theAudioLoop = new Audio();
	theAudioLoop.src = "audio/audioloop.m4a";
	theAudioLoop.style.display = "none";
	document.body.appendChild(theAudioLoop);
	
	theAudioLoop.loop = false;
	theAudioLoop.volume = Math.min(1, window.iTunes.volume);
	theAudioLoop.play();
}

And here we finally get to the first juicy parts. First you will see we create a new Audio object, initialize it with the audio file we want to play back. Tell the HTML that we don't want the audio player to be visible, and add it to our document. We want it to play just once and not loop. We set the volume. And then we tell it to start playback.

The line where we set the volume is interesting because it introduces us to the window.iTunes object. This object is the interface through which we can communicate with iTunes and the music library. In this example we use it to set the volume of the background audio to the same level as the playback volume set in iTunes, but in future tutorials we will use this object to search for tracks and to create and play back temporary playlists, videos, monitor trackchanges and much more.