Embed AVI as a resource file in Delphi

259 views Asked by At

I have a TForm with a TPanel which is linked to the display properties of a TMediaPlayer. By selecting Project -> Resources and Images I was able to insert my video as a resource file, where

  • filename = abc.avi
  • type = RCDATA
  • identifier = Resource_1
unit uForm2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
  Vcl.Dialogs, Vcl.ExtCtrls, Vcl.MPlayer, Vcl.ComCtrls, Mmsystem;
    
type
  TForm2 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form2: TForm2;

implementation

uses
  ShellAnimations;

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
var
  fs: TFileStream;
  rs: TResourceStream;
  s : String;
  m : TMediaPlayer;
begin
  rs := TResourceStream.Create(hInstance, 'Resource_1', RT_RCDATA);
  s  := ExtractFilePath(Application.ExeName) + 'abc.avi';
  fs := TFileStream.Create(s, fmCreate);
  rs.SaveToStream(fs);
  fs.Free;

  MediaPlayer1.Close;
  MediaPlayer1.FileName := s;
  MediaPlayer1.Open;
  MediaPlayer1.Play;
  MediaPlayer1.Display := Panel1;
end;

When the code is compiled, I get an error:

There is no driver installed in the system

Actually, the "abc.avi" file is 1 MiB. If I use a 1 GiB AVI, I get an error:

The file is being used by another process

How can I play this AVI correctly as a Delphi resource? The AVI in both cases has no sound. If I use a TOpenDialog, the video is played, but I don't want the user to select anything. The video must be embedded in the compiled executable.

______________ Updated code and error messages ______________

TMediaPlayer property: MediaPlayer1.DeviceType = dtAVIVideo

Reported 4 errors:

1 [dcc32 Error] uForm2.pas(56): E2010 Incompatible types: 'NativeUInt' and 'string'

  • Line: Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');

2 [dcc32 Error] uForm2.pas(56): E2035 Not enough actual parameters

  • Line: Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');

3 [dcc32 Error] uForm2.pas(98): E2026 Constant expression expected

  • Line: ccRES: FOURCC = MAKEFOURCC(Ord('a'), Ord('v'), Ord('i'), Ord(' '));

4 [dcc32 Error] uForm2.pas(146): E2089 Invalid typecast

  • Line: mmioInstallIOProc(ccRES, TFNMMIOProc(MyResourceIOProc), MMIO_INSTALLPROC or MMIO_GLOBALPROC);
unit uForm2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.MPlayer, Vcl.ComCtrls, Mmsystem;

type
  TForm2 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses ShellAnimations;

{$R *.dfm}

function MAKEFOURCC(ch0, ch1, ch2, ch3: BYTE): FOURCC;
begin
  Result := DWORD(ch0) or (DWORD(ch1) shl 8) or (DWORD(ch2) shl 16) or (DWORD(ch3) shl 24);
end;

function MyResourceIOProc(lpMMIOInfo: PMMIOInfo; uMessage: UINT; lParam1, lParam2: LPARAM): LRESULT; stdcall;
var
  Res: TResourceStream;

  function GetResourceStream: TResourceStream;
  begin
    Move(lpMMIOInfo.adwInfo, Result, SizeOf(TResourceStream));
  end;

  procedure SetResourceStream(Stream: TResourceStream);
  begin
    Move(Stream, lpMMIOInfo.adwInfo, SizeOf(TResourceStream));
  end;

begin
  case uMessage of
    MMIOM_OPEN: begin
      try
        Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');
      except
        SetResourceStream(nil);
        Exit(MMIOERR_CANNOTOPEN);
      end;
      SetResourceStream(Res);
      lpMMIOInfo.lDiskOffset := 0;
      Exit(MMSYSERR_NOERROR);
    end;
    MMIOM_CLOSE: begin
      Res := GetResourceStream;
      SetResourceStream(nil);
      Res.Free;
      Exit(MMSYSERR_NOERROR);
    end;
    MMIOM_READ: begin
      Res := GetResourceStream;
      Move((PByte(Res.Memory) + lpMMIOInfo.lDiskOffset)^, Pointer(lParam1)^, lParam2);
      Inc(lpMMIOInfo.lDiskOffset, lParam2);
      Exit(lParam2);
    end;
    MMIOM_SEEK: begin
      case lParam2 of
        SEEK_SET: begin
          lpMMIOInfo.lDiskOffset := lParam1;
        end;
        SEEK_CUR: begin
          Inc(lpMMIOInfo.lDiskOffset, lParam1);
        end;
        SEEK_END: begin
          Res := GetResourceStream;
          lpMMIOInfo.lDiskOffset := Res.Size - 1 - lParam1;
        end;
      end;
      Exit(lpMMIOInfo.lDiskOffset);
    end;
  else
    Exit(MMSYSERR_NOERROR);
  end;
end;

const
  ccRES: FOURCC = MAKEFOURCC(Ord('a'), Ord('v'), Ord('i'), Ord(' '));


procedure TForm2.FormCreate(Sender: TObject);

begin
 mmioInstallIOProc(ccRES, TFNMMIOProc(MyResourceIOProc), MMIO_INSTALLPROC or MMIO_GLOBALPROC);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
   mmioInstallIOProc(ccRES, nil, MMIO_REMOVEPROC);
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  MediaPlayer1.FileName := 'Resource_1.avi+';
  MediaPlayer1.Open;
  MediaPlayer1.Display:=Panel1;
  MediaPlayer1.Play;
end;

end.
0

There are 0 answers