Introduction
After looking for a simple, yet free MP3 player, I decided to make my own. I based mine on a commercial package that shall remain nameless [Let's just say it that package was expensive considering it was importing the winmm.dll.] Almost every example I found for playing audio file [in my case MP3s] using the winmm.dll only had play/stop capabilities (and sometimes pause). That is fine, I just wanted a bit more, and still wanted to keep it simple. Of course, if I had wanted a full blown MP3 player, I probably would have downloaded one of the many free applications out there. That wasn't my purpose.
So the code is fairly simple. It uses the winmm.dll from Windows to play, stop, pause, etc. It also controls the volume of the left/right channels (if there are more than 1). Moreover, I had fun trying my hand at parsing XML, so the application gets audio file information from a Windows Media Playlist [Making one is easy, just use Windows Media Player].
Just please keep in mind, that I'm not a programmer by profession and this is my first contribution to The Code Project. I know that I'm not re-inventing the wheel here, just hoping this will help someone somewhere.
The project has a couple of files:
- Player.cs [which has all of the winmm.dll
string
commands]: More can be found here, which in my opinion is a pain to understand sometimes. - readPlaylist.cs: This opens up an open dialog window and helps you select a *.wpl file.
- MainPlayer.cs: Basically the buttons and click events, etc.
Player.cs
I tried putting as many relevant comments in the code as I code without going overboard like I'm doing writing this article.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace MP3player
{
class Player
{
private string Pcommand;
private bool isOpen;
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,
StringBuilder strReturn, int iReturnLength, int bla);
public Player()
{
}
public void Close()
{
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, 0);
isOpen = false;
}
public void Open(string sFileName)
{
Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendString(Pcommand, null, 0, 0);
isOpen = true;
}
public void Play(bool loop)
{
if (isOpen)
{
Pcommand = "play MediaFile";
if (loop)
Pcommand += " REPEAT";
mciSendString(Pcommand, null, 0, 0);
}
}
public void Pause()
{
Pcommand = "pause MediaFile";
mciSendString(Pcommand, null, 0, 0);
}
public string Status()
{
int i = 128;
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
mciSendString("status MediaFile mode", stringBuilder, i, 0);
return stringBuilder.ToString();
}
public int LeftVolume
{
get
{
return 0;
}
set
{
mciSendString(string.Concat
("setaudio MediaFile left volume to ", value), null, 0, 0);
}
}
public int RightVolume
{
get
{
return 0;
}
set
{
mciSendString(string.Concat
("setaudio MediaFile right volume to ", value), null, 0, 0);
}
}
public int MasterVolume
{
get
{
return 0;
}
set
{
mciSendString(string.Concat
("setaudio MediaFile volume to ", value), null, 0, 0);
}
}
public int Bass
{
get
{
return 0;
}
set
{
mciSendString(string.Concat
("setaudio MediaFile bass to ", value), null, 0, 0);
}
}
public int Treble
{
get
{
return 0;
}
set
{
mciSendString(string.Concat
("setaudio MediaFile treble to ", value), null, 0, 0);
}
}
}
}
Now for reading the *.wpl file to get MP3s [or whatever] to play.
readPlaylist.cs
using System;
using System.Collections;
using System.Text;
using System.Xml;
namespace MP3player
{
class readPlaylist
{
private ArrayList name = new ArrayList();
private string m_xmlFile;
public string playListPath
{
get
{
return m_xmlFile;
}
set
{
m_xmlFile = value;
Makeplaylist();
}
}
public ArrayList PlayList
{
get
{
return name;
}
}
private void Makeplaylist()
{
XmlTextReader readList = new XmlTextReader(m_xmlFile);
while (readList.Read())
{
if (readList.NodeType == XmlNodeType.Element)
{
if (readList.LocalName.Equals("media"))
{
name.Add(readList.GetAttribute(0).ToString());
}
}
}
}
}
}
Let's use the code now.
Here's a part of the file with all the click events and so on.
So here's the start, make sure you got all the references...
MediaPlayer.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MP3player
{
public partial class MainPlayer : Form
{
ArrayList nowPlaylist = new ArrayList();
Player pl = new Player();
public MainPlayer()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
lstBoxPlayList.Items.Clear();
openPlaylistDialog.ShowDialog();
readPlaylist readList = new readPlaylist();
readList.playListPath = openPlaylistDialog.FileName;
nowPlaylist = readList.PlayList;
for (int x = 0; x < nowPlaylist.Count; x++)
{
lstBoxPlayList.Items.Add(nowPlaylist[x]);
}
lstBoxPlayList.SetSelected(0, true);
}
The above code is pretty straight forward. The btnOpen_click
event will open an OpenFileDialog
window, get the selected *.wpl file and send it to the readPlaylist
class, which will in turn parse through it, return an Arraylist
with all the file names and their paths. Once that is done, loop through it to display its content [in this case a listbox
]. Now you have all your files ready to be played, all that's needed is to select one and press play. Or double-click on it to start it (I won't show that part here).
To play the selected file in the listbox
:
private void btnPlay_Click(object sender, EventArgs e)
{
if (lstBoxPlayList.Items.Count > 0)
{
pl.Open(lstBoxPlayList.SelectedItem.ToString());
pl.Play(false);
}
}
This is a simple way of checking that a file is really selected before playing.
Besides that, pretty simple. Of course, if you've never done this, it's not. It's called the learning process.
As mentioned above, I think the comments in the code are pretty good. With that and the MSDN link that's at the top... you should do fine.
Another thing, I also tried my hand at controlling the Bass/Treble. Don't know if it works, since the speaker system on my computer isn't great, and I'm hard of hearing [hence the reason my speaker system is crappy]. But if I read the MSDN right, it seems to work the same way as the volume control.
Guess that's about it, hope this little example will help someone out.
History
- 9th November, 2006: Initial post