Click here to Skip to main content
6,595,444 members and growing! (21,235 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » Beginners     Intermediate License: The Code Project Open License (CPOL)

Wii-ndows Media Player

By Yousef wadi

Using Wiimote and WiimoteLib to control Windows Media Player
C# (C# 2.0, C# 3.0), Windows (WinXP, Vista), Visual Studio (VS2008)
Posted:19 Jan 2008
Views:15,138
Bookmarked:24 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.36 Rating: 2.85 out of 5

1
1 vote, 33.3%
2

3
1 vote, 33.3%
4
1 vote, 33.3%
5

Introduction

Most of you are familiar to the Nintendo Wii, and if you know the wii you probably know the wiimote (The Wii remote). The Wiimote is one of the most interactive gaming remotes I have put my hands on. After snooping around the net it turned out that using Bluetooth and the wiimote library you can connect the wii to your PC as an HID and send/receive messages form and to the wiimote. In this project we will connect the wiimote and then take control of the Windows Media Player, so the wiimote is going to be controlling the Windows Media Player (WMP) as a simple Bluetooth remote.
THIS PROJECT IS CREATED USING VS2008 so it might not work on previous versions.
You can download VS2008 Express edition for free.

Background

You will have to know how to use C#.NET, we will be using libraries (WiimoteLib) and a Library i created to control the Windows Media Player. It would be good if you know how to basicly connect the wiimote to the PC using bluetooth (It was tested using BlueSoleil application)

Using the code

Lets start off talking about the Structure of the application. Its made up of 3 Parts, 3 Projects within one solution.
-The first part is the Wiimote Library (WiimoteLib)
-The second part is a Class Library I created so that you can control the Windows Media Player(WMP), This was abit tricky since Interoping was required to build this class.
-The Third part is the accual Windows Form Application, and it will be the median between the previous classes.

Part One, WiimoteLib

WiimoteLib is a library that is pre-cretead and consumes the wiimote rather well. You can search for many examples on how to use it.
Ill give a simple explanation on it.
First of all to use this library you should create an instance. Wiimote wm = new Wiimote;
After creating this instance you should connect to the Wiimote, Make sure the wii is connected to your laptop via Bluetooth,

To connect to the laptop you should do the following. When using BlueSoliel turn on bluetooth on the laptop,
pick up you wiimote and click buttons 1 and 2 togther and that will cause the LEDs on the wii to blink rapidly,
now go to Blue Soliel again and search for the wii, when found connect to it. Make sure while you search for the wii and connect that
the LEDs are blinking.

Now connecting the wii thru the application you should use, wm.Connect();
Of courseif you connect somthing you should disconect it, so dont forget to add wm.Disconnect(); when your application terminates.
Now we have a connection ready to send and recive data.

Now we need an event handler for when the wiimote changes its state, and changing its state here is like changing the Button States,
or the Accel States (Compas Sensors). In our project we will only be tracking Button States. So we need to tell the wiimote that by
wm.SetReportType(Wiimote.InputReport.ButtonsAccel,true); and that tells it to use the Buttons and Accel.
Now we event handle the changes by the following

//...Form Load Function...
wm.OnWiimoteChanged += new WiimoteChangedEventHandler(wm_OnWiimoteChanged);
//...
       void wm_OnWiimoteChanged(object sender, WiimoteChangedEventArgs args) //Function fires on wiimote change state change
        {
        }

Now within the event handling we check the wiimote state from the properties within wm.WiimoteState thru if statments
and we can then do what ever we want.
So thats a crash course in WiimoteLib

Part Two, WMPControlYJW

WMPControlYJW is the Windows Media Player Controler class, or we can call it the automater, This was the tricky part,
I searched the net up and down for a pre-made class to control WMP but no luck, so I had to go back to INTEROP, I know its a pain but I got no other choice.
By using Spy++ tool and following the commands one by one I finally created the library, It supports
-Play
-Pause --> Buggy
-Stop
-Next
-Previous
-Volume Up
-Volume Down
-Fast Forward -->Buggy
-Mute
As you can see 2 of the features are marked buggy. Even thought i worked very hard and made sure the commands are correct I still have trouble with both of them.
Pause when called it pauses but when playing again it skips the song and goes to the next.
Fast Forward Jumps to the next song and dosnt Fast Forward. Let me know if you can find away around this.

Now how to use the Class:
First you need to connect to the Open Instance of Windows Media Player by calling the --.Connect()
then simply call the methods you want to use, PLAY,STOP or whatever you can see using VS's Built in intelesense.

Part Three, The GUI, The Median

What does the Median mean? Its the application that picks up the wiimote state change and sends it to the WMPControlYJW
Library that sends it to the WMP and controls it.
Ill Place parts of the codes and explain them.

        //Create Wiimote instance and WMPYjw Control
        private Wiimote wm = new Wiimote();
        private WMPYJW wmp = new WMPYJW();
        //

        //Bools to track previous state of buttons
        private bool ButtonA = false;
        private bool ButtonB = false;
        private bool ButtonUp = false;
        private bool ButtonDown = false;
        private bool ButtonPlus = false;
        private bool ButtonMinus = false;
        private bool Button1 = false;
        private bool ButtonRight = false;
        ////

The first 2 lines of code are creating instances of both libraries, Wiimote and WMPControlYJW.
What are all those bools? They are there to hold the previous state of the wiimote button so as to prevent constant firing
of the IF branch for the current button within the event handler.

 private void Form1_Load(object sender, EventArgs e)
        {
            wmp.ConnectWMP(); // Connect to the Open Windows Media Player
            wm.Connect(); //Connect to Wiimote
            wm.SetReportType(Wiimote.InputReport.ButtonsAccel,true); //Set Needed tracking of wii
            wm.OnWiimoteChanged += new WiimoteChangedEventHandler(wm_OnWiimoteChanged); // Set event for Wiimote change
        }


This is the Form Load, The Projects form. When it runs it connects to the WMP, then Connects to the Wiimote, After that
as explained before it tells the wii which Inputs we want to use.
now we have the wii event and telling it where the function to target is. When the wii state changes it goes to the wm_OnWiimoteChanged Function.

        void wm_OnWiimoteChanged(object sender, WiimoteChangedEventArgs args) //Function fires on wiimote change state change
        {
            #region Automate Play on WiiA   
            if (wm.WiimoteState.ButtonState.A && ButtonA != true) //Checks to prevent multi-firing
            {
                wmp.Play(); // Send play command thru WMPControlYJW 
                ButtonA = true; //To prevent Multi-Firing 
            }
            if (ButtonA == true && wm.WiimoteState.ButtonState.A != true)//To prevent Multi-Firing 
            {
                ButtonA = false;//To prevent Multi-Firing 
            }
            #endregion 
            #region Automate Stop on WiiB
            if (wm.WiimoteState.ButtonState.B && ButtonB != true)
            {
                wmp.Stop();// Send Stop command thru WMPControlYJW 
                ButtonB = true;
            }
            if (ButtonB == true && wm.WiimoteState.ButtonState.B != true)
            {
                ButtonB = false;
            }
            #endregion 
            #region Automate Next on WiiUp
            if (wm.WiimoteState.ButtonState.Up && ButtonUp != true)
            {
                wmp.Next();// Send Next command thru WMPControlYJW 
                ButtonUp = true;
            }
            if (ButtonUp == true && wm.WiimoteState.ButtonState.Up != true)
            {
                ButtonUp = false;
            }
            #endregion 
            #region Automate Previous on WiiDown
            if (wm.WiimoteState.ButtonState.Down && ButtonDown != true)
            {
                wmp.Previous();// Send Previous command thru WMPControlYJW 
                ButtonDown = true;
            }
            if (ButtonDown == true && wm.WiimoteState.ButtonState.Down != true)
            {
                ButtonDown = false;
            }
            #endregion 
            #region Automate Volume Up on WiiPlus
            if (wm.WiimoteState.ButtonState.Plus && ButtonPlus != true)
            {
                wmp.Volume_Up();// Send Volume Up command thru WMPControlYJW 
                ButtonPlus = true;
            }
            if (ButtonPlus == true && wm.WiimoteState.ButtonState.Plus != true)
            {
                ButtonPlus = false;
            }
            #endregion 
            #region Automate Volume Down on WiiMinus
            if (wm.WiimoteState.ButtonState.Minus && ButtonMinus != true)
            {
                wmp.Volume_Down();// Send Volume Down command thru WMPControlYJW 
                ButtonMinus = true;
            }
            if (ButtonMinus == true && wm.WiimoteState.ButtonState.Minus != true)
            {
                ButtonMinus = false;
            }
            #endregion 
            #region Automate Volume Mute on WiiMute
            if (wm.WiimoteState.ButtonState.One && Button1 != true)
            {
                wmp.Mute();// Send Mute command thru WMPControlYJW 
                Button1 = true;
            }
            if (Button1 == true && wm.WiimoteState.ButtonState.One != true)
            {
                Button1 = false;
            }
            #endregion 
            #region Automate Fast Forward on WiiMute
            if (wm.WiimoteState.ButtonState.Right && ButtonRight != true)
            {
                wmp.Fast_Forward();// Send Mute command thru WMPControlYJW // Still Causing Trouble
                ButtonRight = true;
            }
            if (ButtonRight == true && wm.WiimoteState.ButtonState.Right != true)
            {
                ButtonRight = false;
            }
            #endregion   
            

        }

The previous code is how the wii change is handled. It is commented and pretty straight Forward.
Just to put to the point that the bools are used here to prevent multi-firing.

Points of Interest

Within this project we have seen how to basicly use Wiimote with .NET.
Imagine the Infinite possibilites of projects that can be created using this technology.
The best thing in the Wiimote is the IR Camera that can catch up to 4 diffrent IR instances (IR Infrared).
This can help HIC Developers in creating amazing interactive applications.

Created By: Yousef Jamal Wadi

-German Jordanian University

-Conputer Eng. Software Systems

License

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

About the Author

Yousef wadi


Member
- 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



Occupation: Engineer
Company: Genesis Jo
Location: Jordan Jordan

Other popular DLLs & Assemblies articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralNice implementation Pinmemberctorob4:20 20 Jan '08  
General[Message Removed] Pinmemberstonber8:51 1 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jan 2008
Editor:
Copyright 2008 by Yousef wadi
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project