Click here to Skip to main content
12,073,325 members (70,695 online)
Click here to Skip to main content
Add your own
alternative version


225 bookmarked

DirectShow MediaPlayer in C#

, 16 Dec 2003
Rate this:
Please Sign up or sign in to vote.
This article shows how to play a Media File in a C# Windows Application.

Sample Image


Since this is my first article on The Code Project, I would like to apologize for my bad English. It is based only on some "school english" and a few words which I snapped during my leisure job as a Ski Instructor and a Rafting Guide during my education.

I hope everyone can understand this article. Still if questions should exist, I will gladly answer these. And if someone should find some errors, please send me the correct version of the wrong text - thanks for the help when improving my English knowledge spoke Wink | ;-) .

So, let's start...

This small sample program shows, how simple it is to play a video or an audio track with DirectShow and C#.

The program uses the following controls: MainMenu, ToolBar, StatusBar, Panel, ImageList, and a Timer.

Many of the properties are set using the designer, so I would suggest that you download the project, unless of course you are not a beginner.

On the other side, the sample program includes the DirectShow Interface to play videos or audio tracks.

The program demonstrates the following

  • How to select a media file on the disk, using the OpenFileDialog instance class.
  • How to enable or disable the buttons of the ToolBar Control.
  • How to change the text in the StatusBar Control.
  • How to use a basic Timer.
  • How to use the Events of the Timer Control.
  • How to use the Events of the MainMenu Control.
  • How to use the Events of the ToolBar Control.
  • How to use the Events of the Windows Form.
  • How to play a media file with DirectShow.
  • How to determine, if the media file were finished played.
  • ...

The user interface

Beside the 3 buttons to play and stop a video or an audio track, there are also a menu option to select the desired media file. So, before you can play the desired media file you have to open this file with the "File -> Open..." Command in the MainMenu Control. If the file was duly loaded, it can be played now with the "Play" Button in the ToolBar Control. During playing the video or the audio track, the application shows the current position and the duration of the media file in the StatusBar Control. If the media file were finished played, you can restart playing the media file with the "Play" Button. To select another media file, you can use the "File -> Open..." Command of the MainMenu Control.

The user interface - ToolBar The user interface - MainMenu

With the "Info" Command of the MainMenu Control the Info-Dialog of the application will be displayed.

The Info Dialog

The DirectShow Interface:

To play videos or audio files we use the DirectShow Component of the DirectX Package. With the DirectShow Interface it is very simple to play a video or an audio file, most of the work is doing the Interface for you. You only have to setting up the Interface and calling the Interface methods with the correct parameters.

DirectShow Overview

Unfortunately .NET and C# is not an officially supported platform for DirectX development and will not be until DirectX 9 is released at the end of the year. However we can use DirectX with C# in the meantime by using the Visual Basic type library's that come with version 7 and 8 of the API. This article demonstrate how to use the DirectX VB Type Lib in C#.

Before we can begin a .NET DirectShow application we need to create a reference to the DirectShow COM DLL. So, copy the "Interop.QuartzTypeLib.dll" DLL into your project folder and add a reference to this DLL. In Visual Studio.NET this is done by choosing Add Reference from the project menu. As next choose the "Browse..." Button of the Add reference Dialog and select the DirectShow COM DLL.

The Add Reference Dialog

So, after the reference to the DirectShow COM DLL is created add the following code line to your project, to make the DirectShow Interface Classes visible.

using QuartzTypeLib;

The Code

How to select the media file and create the DirectShow Interface?

After the user pressed the "File -> Open..." Command of the MainMenu Control, a "Open File" Dialog is shown and the user can select the desired media file. In C# this is done by creating a instance of the OpenFileDialog class and call the ShowDialog() function.

OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;" + 
                        "*.wav;*.mp2;*.mp3|All Files|*.*";

if (DialogResult.OK == openFileDialog.ShowDialog())

After the user terminated the dialog with OK, we begin to create the DirectShow Interface and to render the selected media file.

This is done in three steps:

DirectShow Overview

  1. Create the filter graph manager (FGM)
  2. Create the filter graph (through FGM)
  3. Run the graph and respond to event

In the following code you see how to create the filter graph manager and the filter graph:


m_objFilterGraph = new FilgraphManager();

m_objBasicAudio = m_objFilterGraph as IBasicAudio;

	m_objVideoWindow = m_objFilterGraph as IVideoWindow;
	m_objVideoWindow.Owner = (int) panel1.Handle;
	m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
catch (Exception ex)
	m_objVideoWindow = null;

m_objMediaEvent = m_objFilterGraph as IMediaEvent;

m_objMediaEventEx = m_objFilterGraph as IMediaEventEx;
m_objMediaEventEx.SetNotifyWindow((int) this.Handle, WM_GRAPHNOTIFY, 0);

m_objMediaPosition = m_objFilterGraph as IMediaPosition;

m_objMediaControl = m_objFilterGraph as IMediaControl;

With the CleanUp() method we try to delete the old interfaces, if someone exists. Before we can start to render the file we have to create an new instance of the FilterGraphManager with the new method. The RenderFile() method builds a filter graph that renders the specified file. The IBasicAudio Interface is to set the volume and the balance of the sound. With the IVideoWindow Interface you can set the window style, the owner window and the position of video window. This functions are enclosed by a try because, if you render a audio file and you try to set the owner window the method throws an exception. So, to play a audio track we don't need the IVideoWindow Interface and we set the m_objVideoWindow member to null. The IMediaEvent and the IMediaEventEx Interface serves for interception of message, which sends DirectShow to the parent window. With the IMediaPosition Interface the current position of the file can be determined. To start and stop the video or the audio track we use the IMediaControl Interface.

For more information about the Interface read the DirectShow documentation on MSDN.

How to play a media file?

The Play Button

To start a video or an audio track use the Run() method of the IMediaControl Interface.


How to break the media file?

The Pause Button

If you want to break the playing video or audio track just use the Pause() method of the IMediaControl Interface.


How to stop the media file?

The Stop Button

To stop the video or the audio track use the Stop() method of the IMediaControl Interface.


How to get the position and the duration of the media file?

The StatusBar

While the media file is played, we indicate the current position and the length of the file in the StatusBar Control. In addition we read all 100ms the CurrentPosition member of the IMediaPosition Interface over a timer and represent its value in the statusbar. To get the length of the file we read the Duration member of the IMediaPosition Interface.

private void timer1_Tick(object sender, System.EventArgs e)
    if (m_CurrentStatus == MediaStatus.Running)

The timer function calls every 100ms the UpdateStatusBar() method, who is displaying the current position and and the duration of the media files in the panels of the statusbar.

private void UpdateStatusBar()
    switch (m_CurrentStatus)
        case MediaStatus.None   : statusBarPanel1.Text = "Stopped"; break;
        case MediaStatus.Paused : statusBarPanel1.Text = "Paused "; break;
        case MediaStatus.Running: statusBarPanel1.Text = "Running"; break;
        case MediaStatus.Stopped: statusBarPanel1.Text = "Stopped"; break;

    if (m_objMediaPosition != null)
        int s = (int) m_objMediaPosition.Duration;
        int h = s / 3600;
        int m = (s  - (h * 3600)) / 60;
        s = s - (h * 3600 + m * 60);

        statusBarPanel2.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);

        s = (int) m_objMediaPosition.CurrentPosition;
        h = s / 3600;
        m = (s  - (h * 3600)) / 60;
        s = s - (h * 3600 + m * 60);

        statusBarPanel3.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
        statusBarPanel2.Text = "00:00:00";
        statusBarPanel3.Text = "00:00:00";

When is the end of the file reached?

In order to determine, when the file was finished played, we overwrite the WndProc method, to intercept the EC_COMPLETE message, which sends DirectShow to the parent window, when the end of the file is reached.

protected override void WndProc(ref Message m)
    if (m.Msg == WM_GRAPHNOTIFY)
        int lEventCode;
        int lParam1, lParam2;

        while (true)
                m_objMediaEventEx.GetEvent(out lEventCode,
                    out lParam1,
                    out lParam2,

                m_objMediaEventEx.FreeEventParams(lEventCode, lParam1, lParam2);

                if (lEventCode == EC_COMPLETE)
                    m_objMediaPosition.CurrentPosition = 0;
                    m_CurrentStatus = MediaStatus.Stopped;
            catch (Exception)

    base.WndProc(ref m);


  • 19.07.2002 - posted (first version)
  • 26.07.2002 - some changes in design and code


Bugs and comments

If you have any comments or find some bugs, I would love to hear about it and make it better.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Strigl
Austria Austria
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: some mp3s not playing Pin
Michael900019-Jan-10 22:38
memberMichael900019-Jan-10 22:38 
GeneralTo play video using direct show in .net Pin
sarithads28-Sep-09 20:37
membersarithads28-Sep-09 20:37 
GeneralGive Strong Name Pin
divyesh143231-Jul-09 20:22
memberdivyesh143231-Jul-09 20:22 
GeneralRe: Give Strong Name Pin
Mystery12317-Nov-09 23:22
memberMystery12317-Nov-09 23:22 
GeneralVideo Overlay Pin
i-developer of Istanbul15-Jun-09 23:04
memberi-developer of Istanbul15-Jun-09 23:04 
GeneralOperations on playing video Pin
anki1233-Dec-08 0:21
memberanki1233-Dec-08 0:21 
GeneralHere is One FREE, RTSP DirectShow Source Filter with full source code Pin
GUI Developer26-Oct-08 5:52
memberGUI Developer26-Oct-08 5:52 
Questionduration is not correct if playing a vbr mp3 Pin
gl_media24-Sep-08 1:02
membergl_media24-Sep-08 1:02 
Generalcompact framework Pin
Member 210738620-Jun-08 21:36
memberMember 210738620-Jun-08 21:36 
GeneralMuliplt Sample Grabber Pin
kazim bhai11-Jun-08 6:40
memberkazim bhai11-Jun-08 6:40 
GeneralRe: Muliplt Sample Grabber Pin
Member 374705815-Jul-10 3:02
memberMember 374705815-Jul-10 3:02 
Questionhow to control sound Pin
Member 353117223-Apr-08 23:15
memberMember 353117223-Apr-08 23:15 
AnswerRe: how to control sound Pin
superbem17-Jun-08 14:11
membersuperbem17-Jun-08 14:11 
QuestionRe: how to control sound Pin
stu__pearce9-Jul-08 13:12
memberstu__pearce9-Jul-08 13:12 
what if there are multiple audio tracks i.e. multiple sound renderers, how do i adjust individually ?
GeneralEnglish hint Pin
Sentonal20-Mar-08 16:23
memberSentonal20-Mar-08 16:23 
GeneralServer side playlist, only the first video is played Pin
buteskin29-Nov-07 0:38
memberbuteskin29-Nov-07 0:38 
QuestionDirectShow as ASP.NET Web Application Pin
menaka indr19-Nov-07 14:43
membermenaka indr19-Nov-07 14:43 
Questioncan it be run on windows mobiles ? Pin
dinlo16-Nov-07 18:25
memberdinlo16-Nov-07 18:25 
QuestionWhy It can not work on Window Server 2003! Pin
doan038326-Oct-07 0:24
memberdoan038326-Oct-07 0:24 
QuestionIVideoWindow Pin
Kamranit24-Oct-07 20:43
memberKamranit24-Oct-07 20:43 
GeneralTwo sound cards Pin
Ibrahim Dwaikat23-Oct-07 11:25
memberIbrahim Dwaikat23-Oct-07 11:25 
GeneralGreat job!!! Pin
kapiltheripper21-Aug-07 10:01
memberkapiltheripper21-Aug-07 10:01 
QuestionNormalize audio? Pin
guden14-Jul-07 23:24
memberguden14-Jul-07 23:24 
Generaldear Pin
Rabindra Patra10-Jul-07 6:52
memberRabindra Patra10-Jul-07 6:52 
GeneralDoes not support . mov files on MY computer Pin
carlosj_z25-May-07 20:52
membercarlosj_z25-May-07 20:52 
GeneralRe: Does not support . mov files on MY computer Pin
Daniel Strigl25-May-07 21:38
memberDaniel Strigl25-May-07 21:38 
Generalthanks for your codes Pin
pengli07235-Apr-07 16:52
memberpengli07235-Apr-07 16:52 
GeneralRe: thanks for your codes Pin
gajendra kumar bansal16-Feb-09 23:19
membergajendra kumar bansal16-Feb-09 23:19 
GeneralRe: thanks for your codes Pin
Todd Chen14-Apr-09 22:04
memberTodd Chen14-Apr-09 22:04 
QuestionHow can I give a byte array as input to a filter graph in DirectShow ? Pin
sidsidz29-Mar-07 23:36
membersidsidz29-Mar-07 23:36 
Questionmultiple videocard rendering Pin
wjp28-Feb-07 22:59
memberwjp28-Feb-07 22:59 
GeneralNice Program Pin
xlthim12-Feb-07 12:03
memberxlthim12-Feb-07 12:03 
QuestionTransparent surface - urgent help !!!!! Pin
compliedmonkey6-Feb-07 11:03
membercompliedmonkey6-Feb-07 11:03 
GeneralProblem with Video and Form Transparency Key Pin
andymo223-Jan-07 5:47
memberandymo223-Jan-07 5:47 
QuestionException from HRESULT: 0x80040200 Pin
arasoft26-Nov-06 3:44
memberarasoft26-Nov-06 3:44 
QuestionRe: Exception from HRESULT: 0x80040200 Pin
Shaun Hayward22-Nov-07 4:03
memberShaun Hayward22-Nov-07 4:03 
AnswerRe: Exception from HRESULT: 0x80040200 Pin
rcorps19-Jan-09 6:39
memberrcorps19-Jan-09 6:39 
GeneralRe: Exception from HRESULT: 0x80040200 Pin
Potestas30-May-09 7:25
memberPotestas30-May-09 7:25 
Generalcalculate Milliseconds Pin
mm3105-Nov-06 3:42
membermm3105-Nov-06 3:42 
QuestionHow to grab a bitmap from a playback video in C# Pin
jrs@dsl2-Nov-06 11:08
memberjrs@dsl2-Nov-06 11:08 
AnswerRe: How to grab a bitmap from a playback video in C# Pin
MeteorRain22-Nov-06 0:20
memberMeteorRain22-Nov-06 0:20 
Generalsome Miss understanding Pin
hock3419-Sep-06 20:02
memberhock3419-Sep-06 20:02 
QuestionDirectShow and .Net Web application Pin
MilindThakkar29-Aug-06 1:26
memberMilindThakkar29-Aug-06 1:26 
GeneralCleanUp does not work Pin
JanissaryD23-Jul-06 21:37
memberJanissaryD23-Jul-06 21:37 
GeneralRe: CleanUp does not work Pin
xlthim12-Feb-07 12:13
memberxlthim12-Feb-07 12:13 
AnswerRe: CleanUp does not work Pin
jojee829-Sep-08 2:58
memberjojee829-Sep-08 2:58 
GeneralFileDialogOpen Pin
pmahavadi7-Jun-06 21:46
memberpmahavadi7-Jun-06 21:46 
Questionhow can directshow support rtsp protocol Pin
shihongtao5-Mar-06 16:08
membershihongtao5-Mar-06 16:08 
QuestionSelect AudioStream Pin
jcvu25-Feb-06 10:35
memberjcvu25-Feb-06 10:35 
GeneralExtra Video File Information Needed Pin
delinhabit14-Feb-06 1:20
memberdelinhabit14-Feb-06 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160208.1 | Last Updated 17 Dec 2003
Article Copyright 2002 by Daniel Strigl
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid