Click here to Skip to main content
Click here to Skip to main content

Tagged as

Mplayer control by VB.NET used in Veron Media Player

, 10 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Code describing that how we can easily control mplayer in VB.NET Winform application


This article will demonstrate that how we can use mplayer to easily control in VB.NET WinForm application without using any COM/ActiveX control. Download source code is sample part of Veron Media Player. At First , we need mplayer.exe in our main application's start path. It can be downloaded from here


Mplayer is a command line audio & video player. It can play audio or video file using several different output drivers. There were no previous examples of how to use mplayer to play audio or video in VB.NET winform application. C++ examples that were found to control mplayer but it is more easy and reliable if VB.NET program use. This article will show how can we use Mplayer to easily play audio or video files in our applications.    

Screenshot of sample part of Veron media player:  



Process object, Window handle control, Input and Output redirection. 

Using the code  

We will use Process class of System.Diagnostics namespace to launch an instance of mplayer to play files. We'll redirect input & output so that we can send commands to mplayer and get information about file from mplayer.

Here is the code that we use to declare variables and initialize process: 

' To declare Variables :-
Dim args, file, time_length As String
Dim str As Long
Dim ps As Process = Nothing
 ' To initialize process :-
' To initialize process :-
ps = New Process()
ps.StartInfo.ErrorDialog = False
ps.StartInfo.UseShellExecute = False
ps.StartInfo.RedirectStandardInput = True
ps.StartInfo.RedirectStandardOutput = True
ps.StartInfo.FileName = "mplayer.exe"
ps.StartInfo.CreateNoWindow = True

Here, args will hold the arguments that will be passed to process except file name. file will be the name of file that is selected to be opened. ps is the process object which will use it to keep a reference to the mplayer process.    

We will use "-slave" option because we want to run mplayer  in 'slave' mode where it will read commands from another process and do actions accordingly.

Here , -nofs option for 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 PictureBox1, so we get its handle and pass it after -wid.

Now , when a user clicks on select file, a file open dialog will open and after a file is selected, its path is stored in the file variable.  

Here code that to open file :-

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    time_length = ""
    str = 1
    Button1.Text = "Pause"
    Button1.Enabled = True
    TrackBar2.Enabled = True
    TrackBar1.Enabled = True
    End Try
    file = OpenFileDialog1.FileName
    args = "-nofs -noquiet -identify -slave -volume " & _
      TrackBar1.Value & _
      " -nomouseinput -sub-fuzziness 1 -vo direct3d, -ao dsound -osdlevel 0 -wid " _
      & CInt(PictureBox1.Handle.ToInt32)
    ps.StartInfo.Arguments = args & " """ & file & """"
End If

First, We want to make sure that any previous instance of mplayer started by us is closed. So, ps.Kill in try catch used that if the process has already exited or not started, the error does not show. Here timer1 , timer2 & timer3 are used, i will discuss later.

We can write commands to mplayer to control the file that is being played. Each command must end with vbLf. SendCommand function will perform the task of sending commands to mplayer process. 

Public Sub SendCommand(ByVal cmd As String)
        If ps IsNot Nothing AndAlso ps.HasExited = False Then
            ps.StandardInput.Write(cmd & vbLf)
        End If
    End Try
End Sub 

So, we can pass commands and control the file that is being played. Now to play or pause a selected file, the following code is used:

If Button1.Text = "Pause" Then
    Button1.Text = "Start"
    TrackBar2.Enabled = False
    TrackBar1.Enabled = False
    Button1.Text = "Pause"
    TrackBar2.Enabled = True
    TrackBar1.Enabled = True
End If 

Here button1 is used to pause or play the file. So, we can give any slave mode command to mplayer by SendCommand function. All slave mode commands can be found in here

Now, description about timer1. It is used to get file's duration & time position. Following code is used in timer1. 

Dim sLine As String = ""
    sLine = ps.StandardOutput.ReadLine
    If (Not String.IsNullOrEmpty(sLine)) Then
    If sLine.Contains("ANS_") Then
        If str = 1 Then
            Dim b As String = sLine.Replace("ANS_LENGTH=", "")
            Dim b As String = sLine.Replace("ANS_TIME_POSITION=", "")
        End If
    End If
    End If
End Try 

Interval of timer1 & timer2 must be 1 millisecond cause if we use greater than 1 then playing process's problem will arise. Timer1 , 2 & 3 must be start for playing file and stop when pause or stop file. In timer1 process, we can read output file's time position successively 1 millisecond. But mplayer shows file's time position in seconds and we want to see it HH:MM:SS format. Therefore ConvertTimeHHMMSS function was used. Following code is used for that function:    

Public Sub ConvertTimeHHMMSS(ByVal timeInSeconds As Double)
        If timeInSeconds >= 0 Then
            Dim iSecond As Double = timeInSeconds
            Dim iSpan As TimeSpan = TimeSpan.FromSeconds(iSecond)
            Dim time_string As String = ""
            time_string = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" & _
              iSpan.Minutes.ToString.PadLeft(2, "0"c) & _
              ":" & iSpan.Seconds.ToString.PadLeft(2, "0"c)
            If str = 1 Then
                time_length = time_string
                TrackBar2.Maximum = timeInSeconds
                If Not time_length = "" Then
                    str = 0
                End If
                Label1.Text = time_string & " / " & time_length
                TrackBar2.Value = timeInSeconds
            End If
        End If
    End Try
End Sub 

Here, TimeSpan.FromSeconds method is used which returns a timespan that represents a specified number of seconds , where the specification is accurate to the nearest millisecond. Now, Timer2 is used to read line from mplayer. Though this process is used in timer1 but it should be used in timer2 otherwise problem will arise for pausing or starting mplayer. So program in time2 is :

End Try  

Timer3 is used to send command to mplayer for reading file's time position. Code is:

 If str = 1 Then
End If

 Here code for Volume Control Track bar is :-

SendCommand("set_property volume " & TrackBar1.Value) 

 And TrackBar2 as Seek Bar is  :-

Private Sub TrackBar2_MouseDown(ByVal sender As System.Object, _
         ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBar2.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim dValue As Double
        dValue = (Convert.ToDouble(e.X) / Convert.ToDouble(TrackBar2.Width)) * (TrackBar2.Maximum - TrackBar2.Minimum)
        TrackBar2.Value = Convert.ToInt32(dValue)
        SendCommand("seek " & TrackBar2.Value & " 2")
    End If
End Sub

Points of Interest 

Mplayer is a really great command line application program and obviously VB.NET Program is one of the best to control mplayer. However, Mplayer has no audio visualization but it can be possible by using FFT algorithm.


  • 03 September , 2013 - First release.  
  • 09 December , 2013 - Second release.


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


About the Author

United States United States
B.Sc in Electrical & Electronics Engineering
Interest in Power electronics & Computer programming

Comments and Discussions

Questiondisplay unicode subtitle Pinmember4L4K112-Dec-14 8:09 
AnswerRe: display unicode subtitle PinmemberS.M.A.Faisal13-Dec-14 1:58 
GeneralRe: display unicode subtitle [modified] Pinmember4L4K113-Dec-14 7:35 
Questionhow to display subtitle? Pinmember4L4K14-Dec-14 10:37 
AnswerRe: how to display subtitle? [modified] PinmemberS.M.A.Faisal5-Dec-14 1:45 
GeneralRe: how to display subtitle? Pinmember4L4K15-Dec-14 5:06 
GeneralRe: how to display subtitle? PinmemberS.M.A.Faisal5-Dec-14 7:56 
GeneralRe: how to display subtitle? Pinmember4L4K15-Dec-14 8:03 
GeneralRe: how to display subtitle? [modified] PinmemberS.M.A.Faisal5-Dec-14 8:51 
GeneralRe: how to display subtitle? Pinmember4L4K15-Dec-14 9:05 
GeneralRe: how to display subtitle? PinmemberS.M.A.Faisal5-Dec-14 9:08 
GeneralRe: how to display subtitle? Pinmember4L4K15-Dec-14 9:26 
QuestionI need this in C# Pinmember4L4K122-Oct-14 0:52 
AnswerRe: I need this in C# PinmemberS.M.A.Faisal22-Oct-14 1:09 
GeneralRe: I need this in C# Pinmember4L4K122-Oct-14 1:30 
GeneralRe: I need this in C# PinmemberS.M.A.Faisal22-Oct-14 2:54 
GeneralRe: I need this in C# Pinmember4L4K122-Oct-14 7:22 
Generalvery well done. PinmemberMaggie Green22-Sep-14 18:17 
Questionsong list? Pinmembermouse7118-Dec-13 17:04 
QuestionSuggestion to use only one timer PinmemberJohannes Weidenauer1-Nov-13 14:20 
AnswerRe: Suggestion to use only one timer [modified] PinmemberS.M.A.Faisal2-Nov-13 5:56 
GeneralMy vote of 5 PinmemberJohannes Weidenauer30-Oct-13 5:07 
Questionmplayer.exe PinmemberJohannes Weidenauer29-Oct-13 0:22 
AnswerRe: mplayer.exe PinmemberS.M.A.Faisal30-Oct-13 4:12 
GeneralRe: mplayer.exe [modified] PinmemberJohannes Weidenauer30-Oct-13 5:11 
Questionshow video in mirror image PinmemberPatrick McGorty15-Oct-13 22:07 
AnswerRe: show video in mirror image PinmemberS.M.A.Faisal20-Oct-13 9:39 
GeneralRe: show video in mirror image PinmemberPatrick McGorty20-Oct-13 14:46 
GeneralRe: show video in mirror image PinmemberS.M.A.Faisal21-Oct-13 0:48 
GeneralRe: show video in mirror image PinmemberPatrick McGorty21-Oct-13 6:28 
GeneralRe: show video in mirror image PinmemberPatrick McGorty22-Oct-13 3:38 
GeneralRe: show video in mirror image PinmemberS.M.A.Faisal22-Oct-13 5:25 
GeneralRe: show video in mirror image PinmemberPatrick McGorty22-Oct-13 6:38 
GeneralMy vote of 5 PinmemberVolynsky Alex4-Sep-13 3:02 
QuestionA novel approach, gets my 5 PinmvpSacha Barber3-Sep-13 22:39 

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
Web01 | 2.8.150331.1 | Last Updated 10 Dec 2013
Article Copyright 2013 by S.M.A.Faisal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid