Click here to Skip to main content
Click here to Skip to main content

Creating mini media player

By , 21 Apr 2009
Rate this:
Please Sign up or sign in to vote.


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();
           // Set media player vars

           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

  • song_name - the song name

  • active_song - is the song is active on the media player

  • mute - is the song muted

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++)
            {
                //  Override tmp
                tmp_basename = this.song_name.Substring(i);

                //System.Windows.Forms.MessageBox.Show("letter: " + this.song_name[i] + " indexof: " + tmp_basename.IndexOf(@"\").ToString());
                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; }

                //  Set it on Windows Media Player

                //  Fix up

                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;
                //System.Windows.Forms.MessageBox.Show(this.player.Volume.ToString());
            }
        }

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()
        {
            //  Some vars
            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);

            //  Set on list
            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);

            //  Set name

            this.SongNameVar.Text = this.player.Active_BaseSongName();

            //  Botton name is stop
            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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gindi Bar Yahav
Web Developer Pear Serivces
Israel Israel
I starterd learning programin in 15th age with C#.
I started to learn WEB Develop and learn all the basics and JavaScript & AJAX, XML, PHP & ASP.NET & SQL.
Today i am a web developer using PHP 5 OR C# ASP.NET Framework to create expert applications.
I am Zend PHP 5 Certificated Developer (http://zend.com/zce.php?c=ZEND010208&r=231721370), and learning new for the MCPD.

Comments and Discussions

 
GeneralMy vote of 4 PinprofessionalArash K119-Jul-13 23:46 
GeneralMy vote of 1 Pinmemberalrsds21-Sep-09 13:06 
GeneralError Pinmemberalrsds21-Sep-09 13:04 
GeneralRe: Error PinmemberGindi Bar Yahav10-Aug-10 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 21 Apr 2009
Article Copyright 2009 by Gindi Bar Yahav
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid