Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Easy MP3 Recording (also using iTunes)

4.58/5 (4 votes)
28 Aug 2010CPOL4 min read 42.1K   3.7K  
Class library and working program allowing you to easily record MP3, wav and raw pcm files. Can also integrate with iTunes.

Introduction

A year ago, my sister and I wanted to make Mp3 back-up copies of everything we had bought on iTunes. So I wrote this program and posted an article on it. However, the program itself was complicated to use and cumbersome in its code as I never bothered to clean up the code once I had finished programming. However, recently it became necessary for me to produce an easy-to-use version. So, while I was improving the design of the application, I thought I'd improve the code as well. As a result, I have produced a library with three useful classes in it (and some helper classes used internally). The two classes are Recorder and iTunes Recorder Extension. Recorder is the base recording class that uses the Istrib libraries (available from here) to record stuff. This can easily be incorporated into any application that wishes to record stuff. iTunes Recorder Extension is more specific as the name suggests. This class provides an easily used object that links iTunes and its libraries with the Recorder class and Istrib libraries.

Using the Code

To use my code is very simple. You need simply to create a Recorder object or iTunesRecorderExtension object. Then setup a few objects and call one of the Start functions.

For this article, I will deal with the iTunesRecorderExtension object as that is what is used in my application though the Recorder class is available and just as easy to use.

To create an iTunesRecorderExtension object, simply do the following:

C#
iTunesRecorderExtension TheRecorder = new iTunesRecorderExtension(true, "A Folder Path");

There are two parameters:

  • DoUseLog - bool
  • ALogFolderPath - string

DoUseLog should be true if you wish to use my Log class. A Log object is a simple way of reading and writing from a text file but in a log format. When you tell the log to write an entry, it automatically adds the long date and time and splits records up so it can be easily read. If you want to use a log, you must also pass it a Log Folder Path. This is a string containing the path to the folder you wish the log to be stored in. For the iTunesRecorderExtension, there are two logs; the Recorder log which comes from the underlying Recorder object and the Application log which comes from the iTunesRecorderExtension and stores information. The Recorder log records when recording starts of each track and what track it was. Also, any errors that the Recorder object encounters are logged. The Application log records when overall recording starts and finishes and also any errors the Recorder object passes back and its own errors.

To use the class, you must do three things:

  1. Select the SoundCaptureDevice
  2. Give it a folder name to store the recorded track in
  3. Tell it to record

To select a sound capture device, you should use a combo box on screen as working out which device has got sound playing on it and which are simply other unused sound cards, etc. is not supported.

The following code adds the available devices to a combo box and could be put in a load function at the start of the application:

C#
List<object> Objects = new List<object>(); 
for (int i = 0; i < TheRecorder.AvailableDevices.Count; i++) 
{ 
	Objects.Add(TheRecorder.AvailableDevices[i]);
} 
SoundCaptureDevicesCmb.Items.AddRange(Objects.ToArray());

The following code transfers the users' selected device to the iTunesRecorderExtension object:

C#
TheRecorder.Device = (Istrib.Sound.SoundCaptureDevice)
	(SoundCaptureDevicesCmb.Items[SoundCaptureDevicesCmb.SelectedIndex]);

To tell the iTunesRecorderExtension object where to save the recorded track, you need one line of code:

C#
TheRecorder.OutputFolder = OutputFolderBox.Text;

The file name is worked out automatically. The files are also sorted by subfolders. The top layer is the Artist, the next subfolder is the Album and in that the track is stored. (Note: If the Artist and or Album is blank/null, they are set to the text 'Blank'!)

Finally you must tell the iTunesRecorderExtension to record. This can be done in two ways, one records immediately and one waits till a designated time to start recording and another time to stop recording. The second way I developed because it meant I could record overnight without having to stay up!

The first way is as follows:

C#
TheRecorder.StartNow((Istrib.Sound.SoundCaptureDevice)
	(SoundCaptureDevicesCmb.Items[SoundCaptureDevicesCmb.SelectedIndex]));

(Note: Even if you have already set the sound capture device, it must be passed again to make sure that one has been set.)

The second way is just as simple with only two extra parameters, StartTime and EndTime. StartTime is the time (accurate to hour and minute, not day or second) that you want recording to begin. EndTime is the nominal time you want recording to end. Recording is told to stop at this time, however, recording continues until the track playing when the EndTime is reached has finished. This ensures that you don't get only half a track recorded, however, if you are recording long audio books, you may want to work out what time recording will end before setting the EndTime!

The code for starting and ending looks like this:

C#
TheRecorder.StartWaiting(dateTimePicker1.Value, dateTimePicker2.Value, 
			(Istrib.Sound.SoundCaptureDevice)
			(SoundCaptureDevicesCmb.Items
				[SoundCaptureDevicesCmb.SelectedIndex])); 

TheRecorder.StopWaiting();

It's that easy! ;D

Some other properties that I have included are Recording and Waiting. Recording returns whether the application is still recording even if the EndTime has been reached/passed. Waiting returns whether the application is waiting for either a StartTime or EndTime to be reached (no distinction is given).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)