C# is so ridiculously simple that even a beginner can make an MP3 player in no time at all using the free Express edition of Visual C#. I've used the 2010 edition for this tutorial, but other versions will probably work fine too --- 2008 for instance.
OK, it'll help a lot if you spend a few weeks learning basic syntax first, but even if you don't know any C# at all you will probably still be able to follow this tutorial. Just cut and paste stuff.
I've structured this tutorial in question and answer form, so that you can test your knowledge as you go along. See how far you can get without looking at the answers!
The MP3 player we create will be very no-frills, but you can use it as the basis of more elaborate projects.
Let's get started.
Step 1. Create a New C# project
Once you've downloaded the free Express edition of Visual Studio C#, create a new project and change the name of the main form to something nice.
Go to File->New Project.
Select "Windows Form Application", type a name for the project and click OK.
Your new project will be created with a blank form. You can already run it by clicking the green run button. It's not very exciting though. Change the name of the form by clicking on the form and changing the text property in the properties browser on the right-hand side.
Step 2. Add a label and three buttons to the form.
Rename the buttons to "open", "play" and "stop".
Hover your mouse over the toolbox to make it open. It's on the left-hand side. Drag three buttons and a label onto your form.
Select each of the buttons in turn and change their name properties to "open", "play" and "stop". Also change the text properties to meaningful text, like "Open", "Play" and "Stop" respectively.
Move the buttons and the label to nice positions. Resize the form if you feel like it.
Finally, add an "OpenFileDialog" to your form in the same way. It'll appear in its own area below your form when you try to place it on the form.
Step 3. Create a music player class
Create a new class. Import "winmm.dll" and add the prototype for the mciSendString function.
Add methods to open a file, start play and stop play.
OK, no-one will blame you if you can't do this without looking at the answer. You sort of either happen to know it or you don't. Even if you've been writing C# for a while, there's no reason why you should know this stuff necessarily.
Go to Project->Add Class. Type a name for the class such as "MusicPlayer" and click OK.
You need to add a DllImport attribute to your class, plus the prototype for mciSendString.
Basically, paste this over your class.
using System.Runtime.InteropServices;
using System.Text;
namespace MP3_Player
{
class MusicPlayer
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
}
}
The DllImport attribute will allow us to access functionality in winmm.dll, allowing us to play mp3s.
This is a bit of a hack really, but there's no way to do this, as far as I know, in ordinary out-of-the-box C#.
Next we'll add stubs for the open, play and stop methods.
using System.Runtime.InteropServices;
using System.Text;
namespace MP3_Player
{
class MusicPlayer
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
public void open(string file)
{
}
public void play()
{
}
public void stop()
{
}
}
}
Now we need to make the methods actually functional. We'll use the mciSendString function to send Windows commands telling it to do stuff with our mp3 file.
Note the "MyMp3" in the command strings; this is just a handle that we've made up for our mp3 file. You can use any handle you like.
using System.Runtime.InteropServices;
using System.Text;
namespace MP3_Player
{
class MusicPlayer
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
public void open(string file)
{
string command = "open \"" + file + "\" type MPEGVideo alias MyMp3";
mciSendString(command, null, 0, 0);
}
public void play()
{
string command = "play MyMp3";
mciSendString(command, null, 0, 0);
}
public void stop()
{
string command = "stop MyMp3";
mciSendString(command, null, 0, 0);
command = "close MyMp3";
mciSendString(command, null, 0, 0);
}
}
}
Step 4: Add handlers to your buttons to control your music player.
Firstly, give your form a MusicPlayer to work with.
Right click "Form1.cs" in the solution explorer on the right and select "view code".
Add a new MusicPlayer instance variable and set it equal to a new music player object.
So your form now looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MP3_Player
{
public partial class Form1 : Form
{
MusicPlayer player = new MusicPlayer();
public Form1()
{
InitializeComponent();
}
}
}
Double-click the open button that you added to your form earlier and add a command to create an "open file" dialog in the handler that C# automatically creates.
Double-click the open file dialog control (in Design View) and add some code that tells the music player to play the opened file. Set the label's text to the filename too.
Be sure to add these bits of code in by double-clicking the controls and then filling in the bit inside the handler --- if you just type these snippets yourself, you'll be missing the code that hooks up the corresponding events to the handlers.
The completed form.cs class looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MP3_Player
{
public partial class Form1 : Form
{
MusicPlayer player = new MusicPlayer();
public Form1()
{
InitializeComponent();
}
private void open_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
label1.Text = openFileDialog1.FileName;
player.open(openFileDialog1.FileName);
}
private void play_Click(object sender, EventArgs e)
{
player.play();
}
private void stop_Click(object sender, EventArgs e)
{
player.stop();
}
}
}
Step 5: Enjoy the music.
That's it. If the above instructions don't work and you're tearing your hair out .... errrm, I'm sorry! They should work. If your project doesn't compile, don't worry, you've just mistyped something somewhere.
If it compiles and runs but you hear no sound, something is badly deranged and I don't know what. Can you play mp3 files OK with Windows Media Player?
Did you add in the button and dialog handlers by double-clicking them? You must do this; if you just paste in the code, your click events won't hook up to your handlers.
Your new mp3 player is certainly rough around the edges, but that's where the fun comes in. You can use it as the basis of your own mp3 player; refine it, sort out any bugs and drawbacks, add features to your heart's content.
More larks?
If you found this page useful, please consider clicking one of the buttons below to tell others about it!
I used to mainly use C++, Perl and Java, but about a year ago I tried C# and was pleasantly surprised. It's really child's play compared to C++.
If you're a beginner, it'll seem confusing at first, but you'll progress quickly. Good luck!