Click here to Skip to main content
15,867,308 members
Articles / Multimedia / DirectX

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 63.1K   3.2K   32   12
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)


Written By
Student
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 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.

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 8:11
Ed Nutting27-Aug-10 8:11 
GeneralDoubt? Manage IPod without Itunes using C# Pin
Anoop Unnikrishnan21-Oct-09 0:33
Anoop Unnikrishnan21-Oct-09 0:33 
GeneralRe: Doubt? Manage IPod without Itunes using C# Pin
Ed Nutting1-Nov-09 6:57
Ed Nutting1-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
Nosa Osayamwen4-Jun-09 13:29 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting21-Jun-09 3:47
Ed Nutting21-Jun-09 3:47 
Ok, try editing code line number 544 - 548. It will look like this:

else
{
OutputAudioFormat = AudioFormat.MP3;
OutputFileExtension = ".mp3";
}

Replace these lines so they look like this:

else if(OutputAudioFormatMp3.Checked)
{
OutputAudioFormat = AudioFormat.MP3;
OutputFileExtension = ".mp3";
}

This seemed to solve the problem however I am not sure why. Anyway, it will continue to record in the selected format via iTunes. (To select the format select one of the radio buttons at the of the form under Output Audio Format).

As to the otehr errors I would request that you download my latest file updates to solve this problem.(It does not contain the above fix so you will need to solve that!)

Sorry for any inconvenience, EdMan196.

Consider the environment, DON'T PRINT!
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting21-Jun-09 3:52
Ed Nutting21-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
Nosa Osayamwen22-Jun-09 9:44 
GeneralRe: Istrib.Sound.Example.WinForms.MainForm2.Start(string)': not all code paths return a value Pin
Ed Nutting26-Jun-09 7:09
Ed Nutting26-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
Nosa Osayamwen26-Jun-09 10:38 
GeneralUrgent File Update!!! Pin
Ed Nutting10-Feb-09 8:03
Ed Nutting10-Feb-09 8:03 
GeneralRe: Urgent File Update!!! Pin
nrgpix3-May-09 14:20
nrgpix3-May-09 14:20 
AnswerRe: Urgent File Update!!! Pin
Ed Nutting22-May-09 23:48
Ed Nutting22-May-09 23: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.