|
|
|
|
|
|
Author Rank:
|
|
Total page views :
46322
|
|
Total downloads :
2087
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
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!
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
|
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.
|
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|