$/-/R!J!T's Blog

Not Just another one on WordPress.com

Simple MP3 Player in C#


Hi! people,
This is my first blog in world of wordpress. I am a student of Computer Engineering from India. I love to learn new programming languages, develop web pages, develop new windows applications and a lot more. Don’t feel that I am a computer nerd. I do many other things but this is the thing that I never get bored off!

Well nice introduction is completed, lets get on to the aim – ‘Our aim is to develop a simple mp3 player in c#.’  This is my first web tutorial so please report any changes that might be required and the most important – your valuable and kind comments!

Steps :-

1. Start VS-2008/2005 (anyone that you have).

Start VS-2008/2005

Start VS-2008/2005

2. Select File -> New Project. Select ‘Windows’ under C# section. From the right side of the window select ‘Windows Forms Application’ and give it a suitable name for ex:- ‘SimpleMP3’.

prj_2

Select project type

3. We will now have the default project settings ready for customization. A default windows form will be displayed in the designer work area. The two other windows of interest are the ‘solutions explorer’ and ‘properties window’. Special attention to Properties window as it greatly simplifies the customization process.

Default project window

Default project window

4. Now we need to customize the form to suit our needs. In the toolbox category expand the common controls section. It contains the most useful controls that we need. Select the ‘Text Box’ control and drag it onto the form surface. A default text box willl appear on the form.

Default control

Default control

Select name from properties window and set the name of text-box to ‘f_txtbox’. Now drag a button on the form, resize it to a suitable size and set it’s name to ‘b_browse’. Set the text property as ‘Browse’. In future, clicking on this button will invoke a file chooser dialog box that will enable us to choose a mp3 file to play.

Button added

Button added

Next add a big button in the lower center and set it’s text property to ‘Play’. Clicking on this button will play the selected song. Set it’s name as ‘b_play’.

Add play button

Add play button

5. Now expand the dialogs category and drag a ‘open file dialog’ on the form. Name it ‘opd’ and set the filter property to ‘MP3 Files|*.mp3’. Next click on Project -> Add Reference, in the dialog box, select browse tab and select wmp.dll of windows/system32 folder.

Add reference

Add reference

Add the following code in the Form1.cs file :-

public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer player;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
player = new WMPLib.WindowsMediaPlayer();
}

private void b_play_Click(object sender, EventArgs e)
{
player.URL = f_txtbox.Text;
player.controls.play();
}

private void b_browse_Click(object sender, EventArgs e)
{
opd.ShowDialog();
f_txtbox.Text = opd.SafeFileName;
}
}

6. This should do the trick. Next save it and start debugging.

Choose the song you want to play and fire play!

Run your player

Run your player

Bingo your player faithfully plays the selected song.

This tutorial was to serve as a introduction to GUI development. This should give you a hang of the common components that are used in GUI development.

You are free to give your comments. Please give your valuable comments as it is the only thing that can help me to improve.

I hope that you have enjoyed this tutorial. In the next tutorial I will take up some other small but useful example for you, till then good bye, happy reading.

Don’t fail to decorate the article with your comments.

October 7, 2009 - Posted by | C#, programming | , , , ,

Sorry, the comment form is closed at this time.