Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Visual C# » Poorman's iPod MP3 Player in C# and .NET

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

This article is an update to an article written by George Steeves on how to create an MP3 player using C# and .NET using the moon valley mp3p.ocx control.

Author Rank :
Page Views : 63547
Downloads : 2982
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PoormansIPod.ZIP
 
 
DevExpress Free UI Controls
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mike Gold

Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems.

He can be reached at mike@c-sharpcorner.com

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
iPor by Scott On January 23, 2007
Pretty slick, certainly offers a painless way to mess with mp3 files. Thanks
Reply | Email | Modify 
Ipod developed please give by Ram On February 6, 2008
This code I am trying I am unable to get the result so..plz kindly send the code in c# exact
Reply | Email | Modify 
ipod by gagan On November 1, 2008
thanks 4 this mp3 player i m very thankful to u
Reply | Email | Modify 
Error,help me??? by Pham On April 19, 2009
The project after build have 3 errors about: The type or namespace name 'AxMP3PLib' could not be found (are you missing a using directive or an assembly reference? Why??Help me
Reply | Email | Modify 
Re: Error,help me??? by pmilin On May 8, 2009
Sounds like you need to register the activeX component. Did you run the setup?
Reply | Email | Modify 
Re: Re: Error,help me??? by ram On July 16, 2010
which setup u are talking about?
please explain.
i am getting same errors . it says are you missing a reference or namespace axmp3plib could not found.
Reply | Email | Modify 
very cool by danny On June 14, 2009
This is very nice, thanks.
I've changed it a little, added better UI and a drag and drop capability and now i'm using this as my new lightweight mp3 player :D

Danny, Locksmith Los Angeles
Reply | Email | Modify 
Re: very cool by nimz On March 10, 2010
hi friends
i wanted to drag & drop function coding for playlist in audioplayer. plz send me dat.
Reply | Email | Modify 
????????????????????????? by warut On March 1, 2010
?????????????????????????
Reply | Email | Modify 
nice job by nimz On March 10, 2010
this is very nice player . thanx man ...its very helpful to me.
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.