Click here to Skip to main content
15,499,046 members
Articles / Multimedia / DirectX
Posted 9 Feb 2009


32 bookmarked

MP3, Wav, and PCM Audio Recorder Using iTunes in C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
21 Jun 2009CPOL3 min read
A program that integrates with iTunes and DirectSound to record music

Image 1


I found that with iTunes, you can play songs in the WMA format, but it doesn't convert them, so on The Code Project website, I found an MP3, WAV, and PCM audio capturing C# code and added all the libraries necessary to capture the audio to the specified format. I was going to use a Windows Media Player class to play the music I wished to capture, but found that it couldn't play many file types, so I went hunting for an alternative. I found the iTunes EXE file under the COM references list and added it straight away. My program now allows you to play a song in iTunes, and it will automatically pick up when it starts, ends, and what the file name is. It will then save it under the specified format (with the correct file extension for that format) in the user specified folder. It is really good, as you can let it play all the songs you like while listening to and re-recording them to a different format. However, you don't have to record from iTunes, nor do you need iTunes installed, as my program has a user start/stop button allowing you to start and stop recording whenever you like.


It will be useful for you to have an understanding of, nothing! So long as you are a good programmer in C# (like myself), this will seem like the easiest program ever written. In some ways, it is, as it requires almost no time or effort as most of the program was already made for me!

(When running the code in VS2008, you will need to turn off 'Lock Loader' errors. To do so, go to Debug menu -> Exceptions -> expand Managed Debug Assistants -> scroll to Lock Loader, and uncheck it. This will have no noticeable side effects to your programming environment, and will hopefully stop the annoying unsolvable errors!

Using the Code

Properties: all the properties and variables are self explanatory. Here are the methods and how they work:

// Not try catch

public MainForm("code-keyword">bool UseiTunes)
    if (UseiTunes)

private void LoadEvents()
        AnIApp.OnPlayerPlayEvent += new 
        AnIApp.OnPlayerPlayingTrackChangedEvent += 
          new _IiTunesEvents_OnPlayerPlayingTrackChangedEventEventHandler(
        AnIApp.OnPlayerStopEvent += new 
    catch (Exception ex)
        DialogResult AResult = MessageBox.Show("Error setting up itunes " + 
                               "event handlers, do you wish to:", 
                               "Error! - What do you wish to do?", 
                               MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
        if (AResult == DialogResult.Abort)
        else if (AResult == DialogResult.Retry)

The above code has the main form constructor and the LoadEvents() method. The MainForm constructor takes the parameter UseiTunes because if iTunes is not installed on the computer the program is running on, then when the user first starts the program, an error will have occurred and the user will have chosen to retry and so the program will not try to set up the iTunes event handlers. The LoadEvents() method simply sets the event handlers for the iTunes events needed. In the try/catch block, if setting the event handlers fails, the user gets the option to retry (which will recall the method and try again), to abort (which will exit the program), or to ignore (which will continue and not try again to set the event handlers - this would cause the iTunes functionality to become unavailable). These try/catch blocks turn up around the code where I thought they were needed. Now you have seen some of the code and how it works. You can download the program or the source files as the rest is fairly self-explanatory.


  • 9th February, 2009: Initial post
  • 10th February, 2009: Updated source files and demo
  • 21st June, 2009: Updated source files


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

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
StaffPlan, Istrib
Poland Poland
I am a .NET developer, working currently in the UK, though still strongly associated with Poland. Primary interests: business application, server-side, enterprise-scale solutions, SOA, thin but rich internet clients (Silverlight).
Apart from my everyday work, I spend a lot of time validating design patterns for newer technologies (like Silverlight, WCF, LINQ). My warzone is - a proof of concept for asynchrony, internet multimedia and command pattern in client-server apps. I also use that website myself as it is a great tool to effectively learn vocabulary (foreign languages), which aids my long being abroad.

Comments and Discussions

GeneralBetter code! Even easier! Class library for recording and iTunes intergration! See new version! Same great results! Pin
Ed Nutting27-Aug-10 9:11
MemberEd Nutting27-Aug-10 9:11 
GeneralDoubt? Manage IPod without Itunes using C# Pin
Anoop Unnikrishnan21-Oct-09 1:33
MemberAnoop Unnikrishnan21-Oct-09 1:33 

I am working on a study project in which my requirement is to build an application similar to Itunes. Is there dll's available for accessing IPhone/Ipod....I need my application to run without any iTune installation.

Best Regards,
GeneralRe: Doubt? Manage IPod without Itunes using C# Pin
Ed Nutting1-Nov-09 7:57
MemberEd Nutting1-Nov-09 7:57 
GeneralIstrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen4-Jun-09 14:29
MemberNosa Osayamwen4-Jun-09 14:29 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting21-Jun-09 4:47
MemberEd Nutting21-Jun-09 4:47 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting21-Jun-09 4:52
MemberEd Nutting21-Jun-09 4:52 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen22-Jun-09 10:44
MemberNosa Osayamwen22-Jun-09 10:44 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting26-Jun-09 8:09
MemberEd Nutting26-Jun-09 8:09 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen26-Jun-09 11:38
MemberNosa Osayamwen26-Jun-09 11:38 
GeneralUrgent File Update!!! Pin
Ed Nutting10-Feb-09 9:03
MemberEd Nutting10-Feb-09 9:03 
GeneralRe: Urgent File Update!!! Pin
nrgpix3-May-09 15:20
Membernrgpix3-May-09 15:20 
AnswerRe: Urgent File Update!!! Pin
Ed Nutting23-May-09 0:48
MemberEd Nutting23-May-09 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.