Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

MusicDrawer: A Simple Music player using WMP SDK

Rate me:
Please Sign up or sign in to vote.
4.95/5 (17 votes)
23 Mar 2013CPOL6 min read 41.6K   6.9K   26  
A Simple Music Player using WMP SDK
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace MusicDrawer.Core
{
    //This enum will keep a track of the current status of the player
    public enum PLAYER_STATUS
    {
        PLAYER_STATUS_NOT_READY,
        PLAYER_STATUS_READY_STOPPED,
        PLAYER_STATUS_PLAYING,
        PLAYER_STATUS_PAUSED,
        PLAYER_STATUS_ENDED,
    };

    //Main Wrapper class around WMPLib
    class MWMPEngine
    {
        //Current status of the player
        PLAYER_STATUS curStatus_;
        
        //Player which will take care of playback for us
        WMPLib.WindowsMediaPlayer player_ = null;

        //Delegate to let the clients know about Event change
        public delegate void OnStatusUpdate(PLAYER_STATUS status);
        
        //Event for clients to subscribe to, if they want to get notfied.
        public event OnStatusUpdate StatusChanged;

        public MWMPEngine()
        {
            //create
            player_ = new WMPLib.WindowsMediaPlayer();

            //stupid hack
            player_.settings.autoStart = false;
            curStatus_ = PLAYER_STATUS.PLAYER_STATUS_NOT_READY;
            player_.StatusChange += new WMPLib._WMPOCXEvents_StatusChangeEventHandler(player__StatusChange);
        }

        void player__StatusChange()
        {
            //For now just handle the "Finished" event, IF we need more we will handle more
            if ("Finished" == player_.status)
            {
                Stop();
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_ENDED);
            }
        }

        //This will be the first step if we want to play something
        public void AttachFile(Song song)
        {            
            if (song != null)
            {                
                player_.currentMedia = player_.newMedia(song.PATH);
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_READY_STOPPED);
            }
        }

        //Lets start playback
        public void Play()
        {
            if (curStatus_ != PLAYER_STATUS.PLAYER_STATUS_PLAYING)
            {
                player_.controls.play();
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_PLAYING);
            }
        }

        //Stop playback
        public void Stop()
        {
            if (curStatus_ != PLAYER_STATUS.PLAYER_STATUS_READY_STOPPED)
            {
                player_.controls.stop();
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_READY_STOPPED);
            }
        }

        //Pause playback
        public void Pause()
        {
            if (curStatus_ != PLAYER_STATUS.PLAYER_STATUS_PAUSED)
            {
                player_.controls.pause();
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_PAUSED);
            }
        }

        //Resume playback
        public void Resume()
        {
            if (curStatus_ == PLAYER_STATUS.PLAYER_STATUS_PAUSED)
            {
                player_.controls.play();
                UpdateStatus(PLAYER_STATUS.PLAYER_STATUS_PLAYING);
            }
        }

        //UI can call this function to keep itself up to date
        public PLAYER_STATUS GetPlayerstatus()
        {
            return curStatus_;
        }

        //Duration of current item
        public double Duration
        {
            get
            {
                return player_.controls.currentItem.duration;
            }           
        }

        //HUman readable duration string
        public string DurationString
        {
            get
            {
                return player_.controls.currentItem.durationString;
            }
        }

        //Current play position
        public double CurruntPosition
        {
            get
            {
                return player_.controls.currentPosition;
            }
            set //Set current position, perhaps from seekbar of UI
            {
                player_.controls.currentPosition = value;
            }
        }

        //Human readable current play position
        public string CurruntPositionString
        {
            get
            {
                return player_.controls.currentPositionString;
            }
        }

        //Update the UI with new status of player
        private void UpdateStatus(PLAYER_STATUS status)
        {
            curStatus_ = status;
            StatusChanged(curStatus_);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions