Click here to Skip to main content
11,805,316 members (63,727 online)
Click here to Skip to main content

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

, , 21 Jun 2009 CPOL 38.4K 2.7K 32
Rate this:
Please Sign up or sign in to vote.
A program that integrates with iTunes and DirectSound to record music


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)


About the Authors

Lukasz Kwiecinski, Istrib
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.

Ed Nutting
United Kingdom United Kingdom

I am Edward Nutting, born March 1996 and I am currently a Computer Science and Electronic Engineering undergraduate at the University of Bristol, UK. I've been self-teaching programming since I was eight years old and have dabbled in many different areas from web development (where I had my own advertising startup) to low level operating system stuff (my current project Fling OS).

The articles and code on this profile are some of my early work (from when I was aged about 13 to 16) but despite my young age, it has been well received by CodeProject users. My more recent work has been for companies or on projects hosted on GitHub, CodePlex or BitBucket.

I keep an eye on many things techy and am open to any and all ideas, so please feel free to contact me if you think I can help! Smile | :)

Ed Nutting

You may also be interested in...

Comments and Discussions

GeneralBetter code! Even easier! Class library for recording and iTunes intergration! See new version! Same great results! Pin
EdMan19627-Aug-10 8:11
memberEdMan19627-Aug-10 8:11 
GeneralDoubt? Manage IPod without Itunes using C# Pin
Anoop Unnikrishnan21-Oct-09 0:33
memberAnoop Unnikrishnan21-Oct-09 0:33 
GeneralRe: Doubt? Manage IPod without Itunes using C# Pin
EdMan1961-Nov-09 6:57
memberEdMan1961-Nov-09 6:57 
GeneralIstrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen4-Jun-09 13:29
memberNosa Osayamwen4-Jun-09 13:29 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
EdMan19621-Jun-09 3:47
memberEdMan19621-Jun-09 3:47 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
EdMan19621-Jun-09 3:52
memberEdMan19621-Jun-09 3:52 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen22-Jun-09 9:44
memberNosa Osayamwen22-Jun-09 9:44 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
EdMan19626-Jun-09 7:09
memberEdMan19626-Jun-09 7:09 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Nosa Osayamwen26-Jun-09 10:38
memberNosa Osayamwen26-Jun-09 10:38 
GeneralUrgent File Update!!! Pin
EdMan19610-Feb-09 8:03
memberEdMan19610-Feb-09 8:03 
GeneralRe: Urgent File Update!!! Pin
nrgpix3-May-09 14:20
membernrgpix3-May-09 14:20 
AnswerRe: Urgent File Update!!! Pin
EdMan19622-May-09 23:48
memberEdMan19622-May-09 23:48 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 21 Jun 2009
Article Copyright 2009 by Lukasz Kwiecinski, Istrib, Ed Nutting
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid