[Mini Project] [Part 2] – Building a Media Player driven solution using Microsoft .Net

 

Continuing the ongoing and exciting Mini Project series with Part 2 – setting up the project…

 

Right, so now that you have Visual Studio and at least Windows Media Player 10 installed, start a new Windows Application Project.  You can call it whatever you like, but I’ve gone with “WindowsMediaRocks”.

 

You can choose any of the languages, but I’ll be doing this in C#. 

 

If I’m feeling particularly generous I’ll do a dual post and provide the relevant Visual Basic.Net code as well, but only by request (in other words: add a comment requesting VB samples).  I hate writing things twice.

 

So, by now you have opened the project and should be staring at a blank form (or ‘dialog’ for us old school Windows platform developers).  If you aren’t then you very probably have some bigger issues with Visual Studio, or, you probably closed it by accident.  Rectify the situation and make sure you are looking at the blank form.

 

This is a good time to personalize the form and the rest of the application.  Because this is a simple proof of concept (POC) I’m not going to either insist or provide instruction, but I’d recommend doing the following:

       Change the name of the form and the name of the default file,

       Give the form a nice icon and title

 

Next, we’re going to add the Windows Media COM (Component Object Model) object to our project so that we can interact with it.  Make sure the toolbox is visible.  If it isn’t click on View->Toolbox (or Ctrl + Alt + X). 

 

Now, right click on the toolbox and select ‘Choose Items…’. This may take some time, so be prepared to be patient.  Once the modal window has been rendered, click on the COM Components tab, then scroll to the bottom and tick the ‘Windows Media Player’ item.  Click OK and you’ll see that a new item has been added to the toolbox under the ‘Common Controls’ panel.

 

Drag and drop the Windows Media Player object onto the form.  Visual Studio takes care of adding all the plumbing for you (which is nice) and now you have a default Media Player object on your form.

 

You can now programmatically control the player object since a nice .Net proxy class has been wrapped around the usually obtuse COM interface API for the media player.

 

Now, double click on the form (not the player object), this will create an event handler for the Form’s load event.  Assuming your Media Player object was named ‘Player1’ (change the name to match the name of your Media Object), you can insert the following statements in the Load event handler:

 

Player1.settings.autoStart = false;

Player1.URL = “[Media Content]”;

 

Note: replace “[Media Content]” (e.g. “C:\\MyVideo.avi”) with the fully qualified path to some audio or video content which you could normally play with Windows Media Player.

 

Now compile and run the application.  You’ll note that the player doesn’t automatically play the content.  Click on the play button – the content specified by [Media Content] should play.

 

Come back for the third post in this series to see how we can muck around with Windows Media Player – we have lots of options at our disposal, but I’m getting tired of typing.

 

/R

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.