Introduction
This article will introduce you to Windows Media Player main classes, how to load a song using C# into your windows form, customize it, making song list etc... this toturial using the Windows Media Player COM refrance.
Background
You may want to read a little about Windows Media Player class or about refrance using COM before reading this article but it not nesse
Using the code
In the start, the media system i wrote used Form1 to initialized, using
private void Form1_Load(object sender, EventArgs e)
{
this.player = new Media_Player_Registry.MediaPlayer();
this.player.Volume = this.Volume.Value;
}
I have called to Media_Player_Registry.MediaPlayer
whose is my media player class that managmanting my actions against Windows Media Player.
in the MediaPlayer
class you can see varibles that will be helpful:
player
- this is the varible that make the "connection" with Windows Media Player COM.
initailized him in the constractor gave me the abillty to control some of the methods that Windows Media Player using (like loading song, rating etc...)
-
Active_Song - active song is a Song type varible ( i made class song too, will descuss it later), this is the pointer of the active song (that i listend to / stoped but the selected)
-
volume
- this is a integet that mean the volume of the song (like you can make it higher and lower in the media player)
-
media_loaded_songs
- integer - number of songs that loaded
The song
class
in the song
class you have a "dummy" class that only store values for the song
Methods
I wont describe all the methods i have made here, but some of them:
the Media_Player_Registry.Song.BaseName()
method:
this is a method that get a basename of a file (I really dont know if there is one in C#... so i made one), the method get the Song_Name
property that looks like this (C:\uses\yahav\Music\song name.mp3) and release from that "song name"
public string BaseName()
{
string basename = "";
string tmp_basename = "";
for (int i = 0; i < this.song_name.Length; i++)
{
tmp_basename = this.song_name.Substring(i);
if (tmp_basename.IndexOf(@"\") == -1)
{
basename += this.song_name[i];
}
}
return basename;
}
Here i got the name, run and substring any time, if i not see any more slashes, starting to save string.
the Media_Player_Registry.MediaPlayer.Volume
property:
public int Volume
{
get { return this.volume; }
set
{
if (value < 0) { this.volume = 0; }
else if (value > 10) { this.volume = 10; }
else { this.volume = value; }
switch ( this.volume )
{
case 0: this.volume = 10; break;
case 1: this.volume = 9; break;
case 2: this.volume = 8; break;
case 3: this.volume = 7; break;
case 4: this.volume = 6; break;
case 6: this.volume = 4; break;
case 7: this.volume = 3; break;
case 8: this.volume = 2; break;
case 9: this.volume = 1; break;
case 10: this.volume = 0; break;
}
this.volume = this.volume * -100;
this.player.Volume = this.volume;
}
}
Its kinda tricky: i realled that the parameter that the Windows Media Player class get is from -100 to -1000, so in my scroller i puted 1 - 10, then i used here ( volume * -100 ) to get it.
why i did that switch? becuse i got that its reverse - if i puted 1 its mean 9... so i didnt thought about a better way...
and the last method i wanted to descuss is Media_Player_Registry.Form1.LoadSong
method:
public void LoadSong()
{
LoadDialog.Reset();
LoadDialog.CheckFileExists = true;
LoadDialog.CheckPathExists = true;
LoadDialog.AddExtension = true;
LoadDialog.DefaultExt = ".mp3";
LoadDialog.Title = "Select mp3 file";
LoadDialog.Filter = "MP3 files (*.mp3)|*.*";
LoadDialog.ShowDialog();
if (this.LoadDialog.FileName == "" || this.LoadDialog.FileName == null)
{
return;
}
this.LoadSong(LoadDialog.FileName);
TreeNode t = new TreeNode();
t.Name = LoadDialog.FileName;
t.Checked = true;
t.NodeFont = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
t.Text = this.player.Active_BaseSongName();
this.SongList.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
t});
}
private void LoadSong(string song_filename)
{
this.player.Load_Song(song_filename);
this.SongNameVar.Text = this.player.Active_BaseSongName();
this.SongState.Text = "Stop";
}
Those are tow methods, one that not get any args, and one that get string filename, the diffrece: the method that not get any args opening a .NET Open File Dialog box, to get the required song from the user, after it get it calls with LoadDialog.FileName (LoadDialog = the name of the dialog box) to the LoadSong with the filename. the LoadSong that not get any args inserting the song into the tree view too
Points of Interest
When i made the volume select, it was REALLY anoying to guess what is their index, so i used code :
System.Windows.Forms.MessageBox.Show( this.player.Volume );
to get the volume.
History
- Beta 1