Hi! This is my first article, so I hope you will ignore my mistakes.
It demonstrates how we can use mplayer to easily display media files in our C# Winform application without using any COM/ActiveX control.
In order to run videos using this code, we need to have mplayer.exe in our bin folder.
It can be downloaded from here. On that page, we also have links for windows build of mplayer.
Prebuilt binaries for mplayer can also be found here.
I wanted to display videos in my C# Winform application, but did not want to use MediaPlayer component.
Mplayer is a command line movie player. It can display video output using several different output drivers. It appeared as a suitable option but there were no examples of how to use mplayer to display output in C# winform application and no front end of mplayer could be found that was made using the .NET framework.
Looking at C++ examples that were found, it turned out that it is fairly simple to do this in C# also. This article will show how can we use Mplayer to easily show video files in our applications.
Here is a screenshot of the sample solution in execution.
The main techniques used in this project are:
- Using Process Object because we are using external EXE to play media files
- Input/Output Redirection because we need to communicate with external process
- Getting Window Handle for a control where we need to display video
Using the Code
We have declared the following variables in our form class:
string args = "";
string filename = null;
Process ps = null;
args will hold the arguments that will be passed to process except file name.
filename will be the name of file that is selected to be opened.
ps is the process object. We'll use it to keep a reference to the mplayer process.
We will use
Process class of
System.Diagnostics namespace to launch an instance of mplayer to play video files.
We'll redirect input so that we can send commands from our application to mplayer process by setting.
Here is the code that we use to initialize process:
ps = new Process();
ps.StartInfo.FileName = "mplayer ";
ps.StartInfo.UseShellExecute = false;
ps.StartInfo.RedirectStandardInput = true;
args = "-nofs -noquiet -identify -slave ";
args += "-nomouseinput -sub-fuzziness 1 ";
args += " -vo direct3d, -ao dsound -wid ";
int id = (int)panel1.Handle;
args += id;
-slave option because we want to run mplayer in 'slave' mode where it will read commands from another process and do actions accordingly.
-nofs option will tell mplayer not to run in full screen mode.
-wid option will tell the id of window where we want to display video output.Here we want to display in panel1, so we get its handle and pass it after
-wid is the most important option to pass and this is what makes sure that the output is displayed in our control instead of another window being created by mplayer.
When a user clicks on Select File, a file open dialog will open and after a file is selected, its path is stored in
OpenFile function is used to play the file. It looks like this:
if (filename == null)
ps.StartInfo.Arguments = args + " \"" + filename + "\"";
SendCommand("set_property volume " + trkVolume.Value);
catch (Exception ex)
We first make sure that any previous instance of mplayer started by us is closed.
try catch so if the process has already exited or not started, the error does no show.
Then we append the path of our selected file to the arguments that will be passed to mplayer.
We will call
start method of our process object to start playing movie.
As a new process is started, so we will send "
set_property" command to mplayer process to set volume according to user selection.
As we have already set
-wid parameter to be the
panel1, so the video will be displayed inside our form in
We can write commands to
stdout of the mplayer process to control the movie that is being played. Each command must end with '
\n' that we append '
\n' to each command instead of using
SendCommand function will perform the task of sending commands to mplayer process.
bool SendCommand(string cmd)
if (ps != null && ps.HasExited == false)
ps.StandardInput.Write(cmd + "\n");
In this way, we can pass commands and control the movie that is being played.
Now to play or pause a selected movie, the following code is used:
if (SendCommand("pause") == false)
pause" command will pause currently playing movie or resume paused movie. In case movie has finished playing, mplayer process would be no longer running. So
SendCommand will return
false. In this case, we will call
OpenFile function which will restart process and start playing the selected movie file.
The rest of the code is fairly simple to understand.
To learn about the commands that can be passed to mplayer in slave mode, you can visit
To learn about switches that are accepted by mplayer.exe, please visit
Points of Interest
If we have movie paused and for some reason our application crashes, we can have a mplayer process running uselessly. By changing the output type of project to console and setting
UseShellExecute=false, we will have a console remaining in case mplayer is not terminated and closing the console will also close mplayer.
In this example, we are just sending data to mplayer process not reading anything back. We can also redirect output of process to read information about currently playing file, like position or duration. Please let me know if you feel it is a good idea to include a seek bar in our demo application to demonstrate this.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.