Skip to content
Peter Gill edited this page Apr 1, 2014 · 10 revisions

This is the easiest way to use mplayer. You load a video and play it. This program is a console application. The example is using SDL as the backend engine. In general on windows you will want to use a different engine depending on whether your program is running on Linux, Mac, or Windows. The video will open in a separate window in this example.

If you were running this in a winforms application you could pass in any controls Handle.ToInt32() when initializing the Player class and the video would play inside of your application.

Console Example

C Sharp

using System;
using LibMPlayerCommon;

class MainClass
{	
	public static void Main (string[] args)
	{
		int handle = (int)System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
		
		MPlayer _play;
		_play = new MPlayer(handle, MplayerBackends.SDL,
			@"\\path\to\mplayer.exe");
		
		_play.Play(@"/path/to/some/video.avi");
			
		Console.WriteLine ("Press enter to exit...");
		System.Console.ReadLine();
	}
}

VB

Imports LibMPlayerCommon

Class MainClass
	Public Shared Sub Main(args As String())
		Dim handle As Integer = CInt(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle)

		Dim _play As MPlayer
		_play = New MPlayer(handle, MplayerBackends.SDL, "\\path\to\mplayer.exe")

		_play.Play("/path/to/some/video.avi")

		Console.WriteLine("Press enter to exit...")
		System.Console.ReadLine()
	End Sub
End Class

On windows you will generally want to use the Direct3D back end. If you do not mplayer will open in its own window instead of the handle you pass in.

Windows Form Example

C Sharp

using System;
using System.Windows.Forms;
using LibMPlayerCommon;


    static class Program
    {
        [STAThread]
        static void Main()
        {
            PlayVideo frm = new PlayVideo();
            frm.Height = 600;
            frm.Width = 800;
           
            Application.Run(frm);
        }
    }

    public class PlayVideo : System.Windows.Forms.Form
    {
        public PlayVideo()
        {
            this.Load += new EventHandler(frm_Load);
        }
        
        private void frm_Load(object sender, EventArgs e)
        {
            int handle = (int)this.Handle; 
           
            MPlayer _play;
            _play = new MPlayer(handle, MplayerBackends.Direct3D, @"\\path\to\mplayer.exe");
            _play.Play(@"/path/to/some/video.avi");
        }
    }

VB

Imports System.Windows.Forms
Imports LibMPlayerCommon


NotInheritable Class Program
	Private Sub New()
	End Sub
	<STAThread> _
	Private Shared Sub Main()
		Dim frm As New PlayVideo()
		frm.Height = 600
		frm.Width = 800

		Application.Run(frm)
	End Sub
End Class

Public Class PlayVideo
	Inherits System.Windows.Forms.Form
	Public Sub New()
		AddHandler Me.Load, New EventHandler(AddressOf frm_Load)
	End Sub

	Private Sub frm_Load(sender As Object, e As EventArgs)
		Dim handle As Integer = CInt(Me.Handle)

		Dim _play As MPlayer
		_play = New MPlayer(handle, MplayerBackends.Direct3D, "\\path\to\mplayer.exe")
		_play.Play("/path/to/some/video.avi")
	End Sub
End Class

Remember that the handle in this example is the handle from the control that the video will be played in. If you are unsure of what control to use place a panel on your form and use the panel handle.