Poorman's iPod MP3 Player in C# and .NET

PoormansIpod.jpg

Figure 1 - Music Player

Introduction

This article is based upon one written on C# Corner by George Steeves on creating a Simple MP3 player.  This article upgrades the player to the .NET 2.0 Framework,  and adds some features to the player, such as the ability to view a song list inside a folder.  It also allows you to play, stop and pause the song.  The download also includes a necessary file (xaudio.dll )  which is required by the mp3p.ocx file developed by moon valley.

Installation

A setup project is included with the download as well as the actual installation of the mp3 player.  Run the installation first so that it registers the activex control and installs the necessary dlls.  You can test out the mp3 player after installing by going to the desktop or the start menu.

The Code

The Poorman's iPod player uses .NET to wrap the mp3p.ocx ActiveX control.  The control has some basic music file capabilities such as the ability to play, pause, or stop your song.  To use the control, you initially need to open the mp3 file into the control.  At this point, you can control the state of the song.  Below is the code for opening a song from the playlist:

Listing 1 - Opening a song into the mp3p.ocx control

/// <summary>
/// Opens a selected song into the mp3 activex control
/// </summary>
private void OpenSelectedSong()
 {
 // make sure the previous output is closed
   axMp3P1.OutputClose();
  //  get the path of the selected mp3 file and open it
  if (lbSongs.SelectedIndex >= 0)
  {
     string songPath = String.Format("{0}\\{1}.mp3", _currentPath, lbSongs.SelectedItem.ToString());
     axMp3P1.InputOpen(songPath);
  }
}

The program allows you to browse for a particular song on your file system.  Initially the program puts you in the "My Music" folder (which is under "My Documents").  If you tend to keep all your music here, then it will be easier to access them.  If you don't keep your music in this folder, you can change the line of code below located inside the constructor of the form

Listing 2 - Setting the starting place of the Folder Browser to the My Music Folder

folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyMusic;

Browsing for the song folder is accomplished through the ShowDialog method of the FolderBrowserDialog as shown in listing 3.  The btnBrowse_click method brings up the folder browser, and upon clicking ok, sets the selected music folder directory in which to look for the songs to play.  Then the method loops through each mp3 file in the selected folder and adds it to the list box.

Listing 3 - Browsing for a song folder

private void btnBrowse_Click(object sender, EventArgs e)
 {
   // open the folder browser
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
     {
         // find out which directory was chosen
         _currentPath = folderBrowserDialog1.SelectedPath;
         DirectoryInfo dir = new DirectoryInfo(_currentPath);
         lbSongs.Items.Clear();
// go through each mp3 file in the directory
// and add it to the list box
     foreach (FileInfo nextFile in dir.GetFiles("*.mp3"))
        {
               lbSongs.Items.Add(nextFile.Name.Remove(nextFile.Name.LastIndexOf('.')));  // also remove the mp3 extension
         }
   // clear the paused flag
         _pausePressed = false;

    }  // end if DialogResult is OK
}

Playing, Pausing, and Stopping the Player

The mp3p.ocx control has methods to play, stop, and pause the player.   To play, simply call Play() as shown in listing 4.  Similarly you can call Pause, to pause the player, and Stop, to stop the player.

Listing 4 - Play Button Event Handler

/// <summary>
/// Plays the current selected song
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
 {
   if (_pausePressed == false)
   {
      OpenSelectedSong();
   }

  axMp3P1.Play();
_pausePressed = false;
}

Conclusion

If you are wondering how to get a hold of mp3 files,  you can either sign up for Napster, or copy the tracks off your existing CDs.  The way I get the tracks off my current CD's into MP3 format is through an add-in to the Windows Media Player that lets me rip tracks into MP3 format. (The latest Windows Media Player may already have mp3 ripping capability without the add-in).  Anyway, enjoy playing your music.  If you have a computer at work, you can use this player with your headphones to enhance your working experience.  Happy Listening!


Similar Articles