Click here to Skip to main content
Click here to Skip to main content

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

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

Introduction

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.

Background

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)
{
    InitializeComponent();
    if (UseiTunes)
    {
        LoadEvents();
    }
}

private void LoadEvents()
{
    try
    {
        AnIApp.OnPlayerPlayEvent += new 
          _IiTunesEvents_OnPlayerPlayEventEventHandler(AnIApp_OnPlayerPlayEvent);
        AnIApp.OnPlayerPlayingTrackChangedEvent += 
          new _IiTunesEvents_OnPlayerPlayingTrackChangedEventEventHandler(
          AnIApp_OnPlayerPlayingTrackChangedEvent);
        AnIApp.OnPlayerStopEvent += new 
          _IiTunesEvents_OnPlayerStopEventEventHandler(AnIApp_OnPlayerStopEvent);
    }
    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)
        {
            Application.Exit();
        }
        else if (AResult == DialogResult.Retry)
        {
            LoadEvents();
        }
    }
}

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.

History

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

License

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 www.nauka-slowek.org - 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
Founder
United Kingdom United Kingdom
Hi there, so you're reading this? I probably ought to tell you something helpful then...
 
Okay well, I'm 17 from London, UK and I have been programming since I was 8 years old. You'll see from my profile I've dabbled in a lot of different areas and I do know what I'm talking about so as they say "gimme some respect yeah?" Smile | :) I've done GCSE's (got 7A*'s, 2A's and a B in French) and am now studying for my AS-Levels (Math, Further Maths, Physics and Chemistry). My main aim is to go to university to study electronic engineering and then work in robotics (preferably humanoid but most of the field is interesting).
 
Thanks for reading,
Edward Nutting
Follow on   Twitter

Comments and Discussions

 
GeneralBetter code! Even easier! Class library for recording and iTunes intergration! See new version! Same great results! PinmemberEdMan19627-Aug-10 8:11 
GeneralDoubt? Manage IPod without Itunes using C# PinmemberAnoop Unnikrishnan21-Oct-09 0:33 
GeneralRe: Doubt? Manage IPod without Itunes using C# PinmemberEdMan1961-Nov-09 6:57 
GeneralIstrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberNosa Osayamwen4-Jun-09 13:29 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberEdMan19621-Jun-09 3:47 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberEdMan19621-Jun-09 3:52 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberNosa Osayamwen22-Jun-09 9:44 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberEdMan19626-Jun-09 7:09 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value PinmemberNosa Osayamwen26-Jun-09 10:38 
GeneralUrgent File Update!!! PinmemberEdMan19610-Feb-09 8:03 
GeneralRe: Urgent File Update!!! Pinmembernrgpix3-May-09 14:20 
AnswerRe: Urgent File Update!!! PinmemberEdMan19622-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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 21 Jun 2009
Article Copyright 2009 by Lukasz Kwiecinski, Istrib, Ed Nutting
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid