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

Wii-ndows Media Player

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
19 Jan 2008CPOL5 min read 45.2K   904   31  
Using Wiimote and WiimoteLib to control Windows Media Player
/*
 * Source Code By Yousef Jamal Wadi
 * WMPControlYJW Library Created by Yousef Jamal Wadi 
 * Feel Free To Distribute as you like ;) 
 * 
 * Yousef Jamal Wadi 
 * Software Arch. and Engineer 
 * German Jordanian University 
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Runtime.InteropServices; // To Work On Introping WMP

namespace WMPControlYJW
{
    public class WMPYJW
    {
        private Int32 wmHandle;
        private const int WM_COMMAND = 0x111;

        //Commands for Windows Media Player
        #region Commands 
        private const int E_B = 0x00000000;

        private const int WM_Play = 0x00014978 ;
        private const int WM_Stop = 0x00014979 ;
        private const int WM_Next = 0x0001497B ;
        private const int WM_Mute = 0x00014981 ;
        private const int WM_VUp = 0x0001497F ;
        private const int WM_VDown = 0x00014980 ;
        private const int WM_Prev = 0x0001497A ;
        private const int WM_FastF = 0x0001497D;
        #endregion  

        // Functions to deal with the API
        #region WIN32 Functions
        ////Dealing with the WIN32////////////////
        [DllImport("User32.dll")]
        public static extern int FindWindow(string strClassName, string strWindowName);

        [DllImport("User32.dll")]
        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string strClassName, string strWindowName);

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(int hWnd,int Msg,int wParam,[MarshalAs(UnmanagedType.LPStr)] string lParam);

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(int hWnd,int Msg,int wParam,int lParam);
        ///////////////
        #endregion  
        
        //Empty Constructor
        public WMPYJW()
        {
        }

        // Connects the library with the Windows Media Player Instance to send commands 
        #region Connect WMP
                public string ConnectWMP()
        {
            try
            {
                wmHandle = FindWindow("WMPLayerApp", "Windows Media Player");
                return(null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion 

        // Sends the play command to the open WMP Window
        #region Play Function
        public string Play()
        {
                try
                {
                    SendMessage(wmHandle, WM_COMMAND, WM_Play,E_B);
                    return (null);
                }
                catch (Exception e)
                {
                    return (e.Message.ToString());
                }
        }        
        #endregion //Function To Send Play Command To WMP

        // Sends the Stop command to the open WMP Window
        #region Stop Function
        public string Stop()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_Stop, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion

        // Send the Next command to the open WMP Window 
        #region Next Function
        public string Next()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_Next, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion
        
        // Send the mute command to the open WMP Window 
        #region Mute Function
        public string Mute()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_Mute, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion

        // Send Volume down command to the open WMP Window
        #region Volume Down Function
        public string Volume_Down()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_VDown, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion

        // Sends Volume Up command to the open WMP Window
        #region Volume Up Function
        public string Volume_Up()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_VUp, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion

        // Sends the Volume Down command to the open WMP Window
        #region Previous Function
        public string Previous()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_Prev, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion

        // Sends the Fast Forward command to the open WMP Window
        #region Fast Forward Function
        public string Fast_Forward()
        {
            try
            {
                SendMessage(wmHandle, WM_COMMAND, WM_FastF, E_B);
                return (null);
            }
            catch (Exception e)
            {
                return (e.Message.ToString());
            }
        }
        #endregion


    }
}

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
Engineer Genesis Jo
Jordan Jordan
- German Jordanian University
Computer Engineering Software Systems
German Jordanian University IT_CLUB President
Genesis Jo Software Arch. and Eng.
Microsoft Student Partner
Microsoft ImagineCup Jordan Champion
Microsoft ImagineCup 2nd Place Regional

- .Net Developer
ASP.NET 3.5
Silverlight (Advanced)
Robotics Studies
3D Virtual Machine Studies



Comments and Discussions