A DirectShow Program

A DirectShow Program in Delphi

To give us an idea of DirectShow, let's start with a simple video project to display a movie file. It's the DirectShow equivalent of the ubiquitous "Hello World!" and is the first example given in the Microsoft documentation. Here is the Delphi version.

Start a new project in Delphi and pop a Button and an OpenDialog on the form. Also, select the FilterGraph component from the DSPack palette and pop it onto the form.

project1

In the Button1Click procedure add the following:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    if not FilterGraph1.Active then FilterGraph1.Active:= True;
    FilterGraph1.RenderFile(OpenDialog1.Filename);
    FilterGraph1.Play;
  end;
end;

Run the project and select a movie (for example AVI) file that you have on your computer. A new window titled 'ActiveMovie' will open and show the movie. Your first DirectShow program!


The separate ActiveMovie window is a little untidy, so add a VideoWindow from the DSPack palette onto the project's form:

project2

We now need to tie the VideoWindow and the FilterGraph together by modifying the Button1Click procedure as follows:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    if not FilterGraph1.Active then FilterGraph1.Active:= True;
    VideoWindow1.FilterGraph:= FilterGraph1;
    FilterGraph1.RenderFile(OpenDialog1.Filename);
    FilterGraph1.Play;
  end;
end;

Run the project again and, this time, the video will play in its own window!

OK, we've left this project a little untidy and relied on Delphi to close it down gracefully. What we should really do is add a second Button (Caption:= 'Stop') and add the following to its Button2Click procedure:

procedure TForm1.Button2Click(Sender: TObject);
begin
    FilterGraph1.Stop;
end;

And, in the form's OnCloseQuery procedure:

    FilterGraph1.ClearGraph;
    FilterGraph1.Active:= False;

I've deliberately raced a bit too far ahead of ourselves here to give you a feel for DirectShow in action but, in the next part, we'll backtrack a little to learn more about the FilterGraph because it's the cornerstone of every DirectShow application.

previous page | next page

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12


This site and its contents are © Copyright 2005 - All Rights Reserved.