Click here to Skip to main content
11,649,406 members (81,943 online)
Click here to Skip to main content

Simple Mplayer Front End in C#

, 3 Apr 2011 CPOL 36.6K 2.9K 25
Rate this:
Please Sign up or sign in to vote.
Code describing how you can simply use Mplayer to display media files inside your winform application

Introduction

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.

Background

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.

ScreenShot of Demo

Techniques Used

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 = "";

/// <summary>
/// Path of selected file
/// </summary>
string filename = null;
/// <summary>
/// The process Object to run Mplayer.exe
/// </summary>

Process ps = null;
  • argswill hold the arguments that will be passed to process except file name.
  • filenamewill be the name of file that is selected to be opened.
  • psis the process object. We'll use it to keep a reference to the mplayer process.

We will use Processclass of System.Diagnosticsnamespace 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();

//Path of Mplayer exe
ps.StartInfo.FileName = "mplayer ";

ps.StartInfo.UseShellExecute = false;
ps.StartInfo.RedirectStandardInput = true;
args = "-nofs -noquiet -identify -slave ";
args += "-nomouseinput -sub-fuzziness 1 ";

//-wid will tell MPlayer to show output inside our panel
args += " -vo direct3d, -ao dsound  -wid ";
int id = (int)panel1.Handle;
args += id;

We use -slaveoption 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.
  • -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 filenamevariable.

The OpenFilefunction is used to play the file. It looks like this:

void OpenFile()
{
    if (filename == null)
        return;
    //Close any current playing media file
    try
    {
        ps.Kill();
    }
    catch
    {
    }
    try
    {
        ps.StartInfo.Arguments = args + " \"" + filename + "\"";
        ps.Start();
        SendCommand("set_property volume " + trkVolume.Value);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

We first make sure that any previous instance of mplayer started by us is closed.
Just wrap ps.Killin trycatch 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 startmethod 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 -widparameter to be the Handleof panel1, so the video will be displayed inside our form in panel1.

We can write commands to stdoutof 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 WriteLinefunction.
SendCommandfunction will perform the task of sending commands to mplayer process.

bool SendCommand(string cmd)
{
    try
    {
        if (ps != null && ps.HasExited == false)
        {
            ps.StandardInput.Write(cmd + "\n");
            return true;
        }
        else
        {
            return false;
        }
    }
    catch
    {
        return false;
    }
}

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)
{
    OpenFile();        
}

The "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 SendCommandwill return false. In this case, we will call OpenFilefunction 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
ftp://ftp2.mplayerhq.hu/MPlayer/DOCS/tech/slave.txt.

To learn about switches that are accepted by mplayer.exe, please visit
http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.txt.

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.

License

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

Share

About the Author

Umair Amin
Software Developer Avance
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionmplayer has delay even it plays a file from my hard disk Pin
4L4K114-Aug-14 18:41
member4L4K114-Aug-14 18:41 
AnswerRe: mplayer has delay even it plays a file from my hard disk Pin
Umair Amin16-Aug-14 6:07
memberUmair Amin16-Aug-14 6:07 
GeneralRe: mplayer has delay even it plays a file from my hard disk Pin
4L4K116-Aug-14 6:46
member4L4K116-Aug-14 6:46 
Questionsubtitle is not shown in mplayer Pin
4L4K18-Aug-14 14:32
member4L4K18-Aug-14 14:32 
AnswerRe: subtitle is not shown in mplayer Pin
Umair Amin9-Aug-14 2:12
memberUmair Amin9-Aug-14 2:12 
GeneralRe: subtitle is not shown in mplayer Pin
4L4K19-Aug-14 15:57
member4L4K19-Aug-14 15:57 
GeneralRe: subtitle is not shown in mplayer Pin
Umair Amin9-Aug-14 16:28
memberUmair Amin9-Aug-14 16:28 
GeneralRe: subtitle is not shown in mplayer Pin
4L4K114-Aug-14 18:37
member4L4K114-Aug-14 18:37 
GeneralRe: subtitle is not shown in mplayer Pin
Umair Amin16-Aug-14 6:07
memberUmair Amin16-Aug-14 6:07 
GeneralRe: subtitle is not shown in mplayer Pin
4L4K126-Aug-14 16:40
member4L4K126-Aug-14 16:40 
GeneralRe: subtitle is not shown in mplayer Pin
Umair Amin26-Aug-14 18:18
memberUmair Amin26-Aug-14 18:18 
GeneralRe: subtitle is not shown in mplayer Pin
4L4K126-Aug-14 23:01
member4L4K126-Aug-14 23:01 
GeneralRe: subtitle is not shown in mplayer Pin
Umair Amin27-Aug-14 22:03
memberUmair Amin27-Aug-14 22:03 
Questionhow to say mplayer to play from specific time and stop at specific time? Pin
4L4K18-Aug-14 9:50
member4L4K18-Aug-14 9:50 
AnswerRe: how to say mplayer to play from specific time and stop at specific time? Pin
Umair Amin8-Aug-14 10:05
memberUmair Amin8-Aug-14 10:05 
GeneralRe: how to say mplayer to play from specific time and stop at specific time? Pin
4L4K18-Aug-14 10:14
member4L4K18-Aug-14 10:14 
GeneralRe: how to say mplayer to play from specific time and stop at specific time? Pin
Umair Amin8-Aug-14 10:52
memberUmair Amin8-Aug-14 10:52 
GeneralRe: how to say mplayer to play from specific time and stop at specific time? Pin
4L4K18-Aug-14 10:58
member4L4K18-Aug-14 10:58 
QuestionHow to switch nofs to fs? Pin
Member 80038287-Jun-13 0:32
memberMember 80038287-Jun-13 0:32 
AnswerRe: How to switch nofs to fs? Pin
Umair Amin23-Jun-13 1:23
memberUmair Amin23-Jun-13 1:23 
GeneralMy vote of 5 Pin
kaedei23-Dec-12 15:07
memberkaedei23-Dec-12 15:07 
great!
GeneralMy vote of 5 Pin
Ricky Subagyo29-Apr-12 1:52
memberRicky Subagyo29-Apr-12 1:52 
Questionhow to write part of application widow to vga/usb out as video stream Pin
jayachanakya14-Mar-12 19:02
memberjayachanakya14-Mar-12 19:02 
AnswerRe: how to write part of application widow to vga/usb out as video stream Pin
Ricky Subagyo29-Apr-12 2:21
memberRicky Subagyo29-Apr-12 2:21 
QuestionHow to read return values and hide cmd window?? Pin
Wade Chang25-Oct-11 23:44
memberWade Chang25-Oct-11 23:44 
AnswerRe: How to read return values and hide cmd window?? Pin
Wade Chang26-Oct-11 0:35
memberWade Chang26-Oct-11 0:35 
AnswerRe: How to read return values and hide cmd window?? Pin
Umair Amin26-Oct-11 3:57
memberUmair Amin26-Oct-11 3:57 
Questionmplayer front end Pin
gpsm19896-Sep-11 23:09
membergpsm19896-Sep-11 23:09 
AnswerRe: mplayer front end Pin
robertico8-Sep-11 5:28
memberrobertico8-Sep-11 5:28 
GeneralMy vote of 5 Pin
robertico29-Aug-11 7:08
memberrobertico29-Aug-11 7:08 
QuestionSeekbar Pin
robertico29-Aug-11 7:03
memberrobertico29-Aug-11 7:03 
AnswerRe: Seekbar Pin
seer_tenedos21-Nov-11 15:35
memberseer_tenedos21-Nov-11 15:35 
GeneralBlack Window Pin
2ndAge7-Jun-11 17:54
member2ndAge7-Jun-11 17:54 
GeneralRe: Black Window Pin
Umair Amin7-Jun-11 19:57
memberUmair Amin7-Jun-11 19:57 
GeneralRe: Black Window Pin
2ndAge8-Jun-11 0:25
member2ndAge8-Jun-11 0:25 
GeneralRe: Black Window Pin
Umair Amin8-Jun-11 1:47
memberUmair Amin8-Jun-11 1:47 
GeneralRe: Black Window [modified] Pin
2ndAge8-Jun-11 3:51
member2ndAge8-Jun-11 3:51 
GeneralRe: Black Window Pin
Umair Amin8-Jun-11 4:57
memberUmair Amin8-Jun-11 4:57 
Questionwhy it can't play rtsp stream? Pin
RestartX14-May-11 20:10
memberRestartX14-May-11 20:10 
AnswerRe: why it can't play rtsp stream? Pin
Umair Amin14-May-11 23:19
memberUmair Amin14-May-11 23:19 
QuestionRe: why it can't play rtsp stream? Pin
RestartX14-Sep-12 17:28
memberRestartX14-Sep-12 17:28 
AnswerRe: why it can't play rtsp stream? Pin
Umair Amin15-Dec-12 0:18
memberUmair Amin15-Dec-12 0:18 
GeneralGreat article Pin
arsal2004110-Apr-11 13:28
memberarsal2004110-Apr-11 13:28 
GeneralMy vote of 5 Pin
arsal2004110-Apr-11 13:28
memberarsal2004110-Apr-11 13:28 
GeneralMy vote of 5 Pin
Johann Krenn4-Apr-11 9:11
memberJohann Krenn4-Apr-11 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150804.4 | Last Updated 4 Apr 2011
Article Copyright 2011 by Umair Amin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid