I had need to be able to play a video file (both mpg and wmv) in a WinForms application recently. I thought it would be rather straightforward, and for the most part it was. However there were a few gotcha's I ran into so I thought I would document them here in case anyone else runs into the issue. Also, if anyone knows a better way please let me know.
One of the conditions I had to meet was that the video file had to play in a loop continuously, as the application is designed as a Kiosk application. Another condition was that I did not want to display any of the UI controls, just the video itself to avoid users stopping or otherwise altering the video playback.
I decided to use the ActiveX (COM) version of the control for simplicity, so I right clicked the Toolbox and selected the Choose Items option and on the resulting dialog box I selected COM Components, scroll down and select Windows Media Player: (Click on the image to see it full size.)
Once that is done we drop the control onto the form, and set it's properties as needed. Some of the properties that I set at design time are shown below:
Notably, I set FullScreen to false, and uiMode to none which means that there will be no visible interface, Play, Stop etc will not be displayed to the user allowing me to have more control over the end user experience.
It is important to note that uiMode is not a drop down in the designer, the relevant values for this property are:
I chose none because I wanted to preserve the option of allowing the interface to be shown for administrative users without messing up the visual layout, none reserves the space necessary to display the UI controls.
With the design time properties set I then wrote the following code to load and play a video file:
1: wmpMain.URL = mConfig.VideoFile
2: wmpMain.settings.setMode("loop", True)
3: wmpMain.settings.autoStart = True
4: wmpMain.Ctlcontrols.play()
mConfig.VideoFile is a string property that contains a fully qualified path and filename and is used to set the file to be loaded. The second line of code was the tricky part for me, searching for various combinations of .NET and play video in a loop etc resulted in lots of examples of embedding the player in a web page with an attribute called "loop" set to true, but I could not figure out how to implement that in a WinForms version.
Eventually I stumbled on the setMode reference which is rather vague when it comes to intellisense. The available setMode settings are:
Syntax: player.settings.setMode(modeName, state)
Parameters
modeName (one of the following)
state - Boolean specifying whether the new specified mode is active or not.
So in a nutshell that is the basics of getting a video (or audio) file to play in a loop in a WinForms application. There is a lot more you can do with the player, you can programmatically create playlists, react to events the player raises etc. But this illustrates the basics.
Cheers,
Bob Porter
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Robert B. Porter - Powered by: newtelligence dasBlog 2.1.8102.813