Introduction

In this article, we will see how we can develop an MP3 player, using C#.NET, which will use some of the new features of Windows 7.

We will show the progress of the song being played using the "Progress bar" in Taskbar. We will also show the Album art of the song as the "Thumbnail" and we will also have a Thumbnail Toolbar which has the Play/Pause, Next and Previous buttons.

Developing the Application

The Player

Add the existing projects Shell and Core which can be found under the WindowsAPICodePack\shell and WindowsAPICodePack\core folders

Add a reference to the above projects and import the following namespaces:

Microsoft.WindowsAPICodePack.Dialogs

Microsoft.WindowsAPICodePack.Taskbar

Microsoft.WindowsAPICodePack.Shell

The first part of the development is developing the player which will have the basic functionality like Play/Pause, Stop, next Previous/Next and Add/Remove songs Buttons. The main form in our application has 7 Buttons, A listbox, Label and a Picture Box.

We will use the Picture Box to display the Album Art and we will use the Label to display the seconds remaining while a song is being played.

Note: Make sure you have added references for Core, Shell, Bass.net and taglib-sharp. Also add bass.dll to your project (Make sure you have set "Copy to Output Directory" as "Copy Always".)

Player Class: This class has all the methods which we will be using to play MP3 files. The first step is to initialize an output device. We use Bass.BASS_Init() for this. Following are the parameters:

-1 denotes default device.

44100 is the frequency.

BASSInit.BASS_DEVICE_DEFAULT denotes th default device to use

System.IntPtr.Zero denotes the application's main window which is the current foreground window.

Before playing a song, we must first create a stream. We use BASS_StreamCreateFile() to create a stream from a file. We then use BASS_ChanelPlay() to play the stream which we created in the LoadSong() function.

Song Length and Album Art

Once the player is ready, we will use Taglib sharp to get the length of the song and album art. We will use the picture box to display the album art. We will also use this picture box's location and size to create the Thumbnail.

Once we create the TagLib file from a file (in this case, the selected item in the playlist listbox), we will use Properties.Duration.TotalSeconds to get the length of the song in seconds. We will be using this value to populate the Label and the set the progressbar value.

We will also be using taglib sharp to get the album art of the song which is being currently played. Some songs may not have an Album art so we will use TagLibfile.Tag.Pictures.Length to get the numbers of pictures associated with the song. If it is more than zero, then we load the picture box with the album art.

This is the main function which will play the song. First we call the SetAlbumArt() function which will set the album art. We use SetTaskBarthumbnail() function to the set the thumbnail. Explanation for this function comes later in this article. We will use GetSongLength() and SetTimerforPlay() to get the song length and activate the timer respectively.

In the below function, we first check whether the song is paused. We use the value of playState for this. Value zero means the song is paused and we use the PlaySong() function to continue playing the song. If the value is not zero, then it denotes that the song should be played from the beginning. In this case, we load the song and then play it.

This function is called just before playing the song to enable the timer and set the progress bar style. TaskbarProgressBarState.Normal denotes that the progress will be normal. Timer's interval is set as 1000 ms.

This is similar to SetTimerforPlay() but we will disable the timer to stop the progress in the progress bar. At the same time, we will also set its style to TaskbarProgressBarState.Error so that the color is set as Red. This function is called when the song is paused.

We will be using TabbedThumbnail.SetThumbnailClip to set the thumbnail for our MP3 player. For this, we create a new Rectangle selection inside the main form and then set it as the thumbnail clip. Our motive is to use the Album art which we have already loaded in the Picture Box. Note: I had to make some adjustments in the Height and Width of the rectangle selection to get a proper thumbnail.

Progressbar

Timer's interval is set to 1000ms and it will be enabled as soon as the play button is pressed. We use the SongLength to set the maximum value of the progressbar. and increment the progress value every second. Timer will be disabled as soon as the pause button is pressed.

Nice one. Reminds me of one of my first apps on which I was very proud! I made it almost 20 years ago, in VB4, Windows 95. I have found an ocx that I used for decoding the mp3's but as it was not optimized it had some performance problems even on my stunning Pentium 133 Mhz with 16MB of RAM!

Great article!! But the only downside it is using Bass library which must be licensed for commercial use.

You can use the open source C++ mp3 class[^] written/modified by me. The caveat is if you build the C++ library in x86 (32 bits) or x64 (64 bits) platform, the .NET application has to be in the same platform, not 'Any CPU' platform.

hi, anyone who has the The exception:
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_NOSPEAKER, System.IntPtr.Zero);

I solved it by downloading bass.dll from "Un4seen.com", seems the one included in the folder is corrupt
the bass.dll in the folder is about 202kb in size while the one from Un4seen website is about 98KB.

Other than that, The article is a straight 5, and the player sounds great too..

Yes replace the Bass.dll inside /player/bin/debug/ folder..OR just delete the bass.dll in the project explorer windows and then select Add->Existing item, and point to the bass.dll you just downloaded. Rebuild the project and it should work.

Click the download next to the windows label, then unzip the contents and copy the bass.dll inside the folder to your project directory. Do not try to add bass.dll as a reference (because it is not a valid assembly/component), just add it as an "existing item" to the project, right click on your project and click 'add' and then 'Existing item', select your bass.dll and the library should then be seen in the list on Project explorer (next the the form and code files). Then rebuild the project.

The easiest method is to replace all the bass.dll files in the folders, with the new one.

Great work with the player. I have a question regarding channel slider. What I want to achieve is to get control to pan channels from left to right and back (balance slider). How if at all it can be done?

I tried to build this project in vs2010 beta 2, and build it successed. When I run the exe file ,there is a exception....The exception throwed from this lien: Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_NOSPEAKER, System.IntPtr.Zero); Someone has this problem?