ViewFD
На главную   Почта

Пример использования DirectShow и MCI

Компилятор: Borland Delphi 7

Операционная система: Windows XP

 

Текст файла Project1.dpr

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


  Текст файла Unit1.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls;

type

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
  public
  end
;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ActiveX, ComObj, DShow, MMSystem;

var
  GraphBuilder: IGraphBuilder; 
  MediaControl: IMediaControl;
  VideoDeviceID: integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CoInitialize(nil)
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  CoUninitialize
end;

procedure PlayMediaDS(FileName: string);
begin
  if CoCreateInstance(CLSID_FilterGraph,nil,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,GraphBuilder)=S_OK then
  if GraphBuilder.RenderFile(StringToOleStr(FileName),nil)=S_OK then
  if GraphBuilder.QueryInterface(IID_IMediaControl,MediaControl)=S_OK then
  MediaControl.Run
end;

procedure PlayMediaMCI(FileName: string);
var
  GenParm: TMCI_Generic_Parms;
  OpenParm: TMCI_Open_Parms;
  PlayParm:TMCI_Play_Parms;
begin
  if VideoDeviceID>0 then
  begin
    ZeroMemory(@GenParm,SizeOf(GenParm));
    if mciSendCommand(VideoDeviceID,mci_Close,mci_Wait,Integer(@GenParm))=0 then
    VideoDeviceID:=0
  end;
  OpenParm.lpstrDeviceType:='';
  OpenParm.lpstrElementName:=PAnsiChar(FileName);
  if mciSendCommand(0,mci_Open,mci_Open_Element or mci_Wait,Integer(@OpenParm))=0 then
  begin
    VideoDeviceID:=OpenParm.wDeviceID;
    ZeroMemory(@PlayParm,SizeOf(PlayParm));
    mciSendCommand(VideoDeviceID,mci_Play,0,Integer(@PlayParm))
  end
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then PlayMediaDS(OpenDialog1.FileName)
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.Execute then PlayMediaMCI(OpenDialog1.FileName)
end;

end.
 

Текст файла Uni1.dfm

object Form1: TForm1
  Left = 400
  Top = 114
  Width = 211
  Height = 74
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 8
    Top = 8
    Width = 75
    Height = 25
    Caption = 'DirectShow'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 88
    Top = 8
    Width = 75
    Height = 25
    Caption = 'MCI'
    TabOrder = 1
    OnClick = Button2Click
  end
  object OpenDialog1: TOpenDialog
    Left = 168
    Top = 4
  end
end

загрузка файла: ds_mci.exe, ds_mci.exe (SFX-архив)

размер: 50 КБ

 

загрузка файла: dshow.exe, dshow.exe (SFX-архив)

размер: 190 КБ

  Вверх