Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / DirectX

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

4.71/5 (4 votes)
21 Jun 2009CPOL3 min read 64.4K   3.2K  
A program that integrates with iTunes and DirectSound to record music

Image 1

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:

C#
// 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)