Difference between revisions of "Using libvlc with Delphi"

From VideoLAN Wiki
Jump to navigation Jump to search
m (See also: ActiveX/Delphi)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This is a sample code to play media files using libvlc with Delphi (test with Delphi XE2 VCL form, VLC 1.1.11).
+
{{See also|ActiveX/Delphi}}
 +
{{example code|for=LibVLC}}
 +
This is a sample code to play media files using [[libVLC]] with Delphi (test with Delphi XE2 VCL form, VLC 1.1.11).
  
 
== Form design  ==
 
== Form design  ==
Line 6: Line 8:
  
 
== Full source code  ==
 
== Full source code  ==
<pre>unit Unit1;
+
 
 +
=== Implementations  ===
 +
<syntaxhighlight lang="delphi">unit Unit1;
 
interface
 
interface
  
Line 40: Line 44:
  
 
var
 
var
   libvlc_media_new_path             &nbsp;: function(p_instance&nbsp;: Plibvlc_instance_t; path&nbsp;: PAnsiChar)&nbsp;: Plibvlc_media_t; cdecl;
+
   libvlc_media_new_path             : function(p_instance : Plibvlc_instance_t; path : PAnsiChar) : Plibvlc_media_t; cdecl;
   libvlc_media_new_location         &nbsp;: function(p_instance&nbsp;: plibvlc_instance_t; psz_mrl&nbsp;: PAnsiChar)&nbsp;: Plibvlc_media_t; cdecl;
+
   libvlc_media_new_location         : function(p_instance : plibvlc_instance_t; psz_mrl : PAnsiChar) : Plibvlc_media_t; cdecl;
   libvlc_media_player_new_from_media&nbsp;: function(p_media&nbsp;: Plibvlc_media_t)&nbsp;: Plibvlc_media_player_t; cdecl;
+
   libvlc_media_player_new_from_media : function(p_media : Plibvlc_media_t) : Plibvlc_media_player_t; cdecl;
   libvlc_media_player_set_hwnd     &nbsp;: procedure(p_media_player&nbsp;: Plibvlc_media_player_t; drawable&nbsp;: Pointer); cdecl;
+
   libvlc_media_player_set_hwnd       : procedure(p_media_player : Plibvlc_media_player_t; drawable : Pointer); cdecl;
   libvlc_media_player_play         &nbsp;: procedure(p_media_player&nbsp;: Plibvlc_media_player_t); cdecl;
+
   libvlc_media_player_play           : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
   libvlc_media_player_stop         &nbsp;: procedure(p_media_player&nbsp;: Plibvlc_media_player_t); cdecl;
+
   libvlc_media_player_stop           : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
   libvlc_media_player_release       &nbsp;: procedure(p_media_player&nbsp;: Plibvlc_media_player_t); cdecl;
+
   libvlc_media_player_release       : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
   libvlc_media_player_is_playing   &nbsp;: function(p_media_player&nbsp;: Plibvlc_media_player_t)&nbsp;: Integer; cdecl;
+
   libvlc_media_player_is_playing     : function(p_media_player : Plibvlc_media_player_t) : Integer; cdecl;
   libvlc_media_release             &nbsp;: procedure(p_media&nbsp;: Plibvlc_media_t); cdecl;
+
   libvlc_media_release               : procedure(p_media : Plibvlc_media_t); cdecl;
   libvlc_new                       &nbsp;: function(argc&nbsp;: Integer; argv&nbsp;: PAnsiChar)&nbsp;: Plibvlc_instance_t; cdecl;
+
   libvlc_new                         : function(argc : Integer; argv : PAnsiChar) : Plibvlc_instance_t; cdecl;
   libvlc_release                   &nbsp;: procedure(p_instance&nbsp;: Plibvlc_instance_t); cdecl;
+
   libvlc_release                     : procedure(p_instance : Plibvlc_instance_t); cdecl;
  
 
   vlcLib: integer;
 
   vlcLib: integer;
Line 56: Line 60:
 
   vlcMedia: plibvlc_media_t;
 
   vlcMedia: plibvlc_media_t;
 
   vlcMediaPlayer: plibvlc_media_player_t;
 
   vlcMediaPlayer: plibvlc_media_player_t;
 +
</syntaxhighlight>
 +
=== Get VLC installation path  ===
  
 +
Normally, the library libvlc.dll is in <code>"C:\Program files\Videolan\VLC\"</code>. This function read registry to get the correct VLC installation path, stored in key <code>HKEY_LOCAL_MACHINE\Software\VideoLAN\VLC</code>.
 +
<syntaxhighlight lang="delphi">
 
// -----------------------------------------------------------------------------
 
// -----------------------------------------------------------------------------
 
// Read registry to get VLC installation path
 
// Read registry to get VLC installation path
Line 67: Line 75:
 
   Key: PWideChar;
 
   Key: PWideChar;
 
begin
 
begin
   Result&nbsp;:= '';
+
   Result := '';
   Key&nbsp;:= 'Software\VideoLAN\VLC';
+
   Key := 'Software\VideoLAN\VLC';
 
   if RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, Handle) = ERROR_SUCCESS then
 
   if RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, Handle) = ERROR_SUCCESS then
 
   begin
 
   begin
Line 75: Line 83:
 
       SetLength(Result, DataSize);
 
       SetLength(Result, DataSize);
 
       RegQueryValueEx(Handle, 'InstallDir', nil, @RegType, PByte(@Result[1]), @DataSize);
 
       RegQueryValueEx(Handle, 'InstallDir', nil, @RegType, PByte(@Result[1]), @DataSize);
       Result[DataSize]&nbsp;:= '\';
+
       Result[DataSize] := '\';
 
     end
 
     end
 
     else Showmessage('Error on reading registry');
 
     else Showmessage('Error on reading registry');
 
     RegCloseKey(Handle);
 
     RegCloseKey(Handle);
     Result&nbsp;:= String(PChar(Result));
+
     Result := String(PChar(Result));
 
   end;
 
   end;
 
end;
 
end;
 +
</syntaxhighlight>
 +
=== Load libvlc library into memory ===
 +
 +
Next, load library libvlc.dll into memory
 +
<syntaxhighlight lang="delphi">
 
// -----------------------------------------------------------------------------
 
// -----------------------------------------------------------------------------
 
// Load libvlc library into memory
 
// Load libvlc library into memory
Line 87: Line 100:
 
function LoadVLCLibrary(APath: string): integer;
 
function LoadVLCLibrary(APath: string): integer;
 
begin
 
begin
   Result&nbsp;:= LoadLibrary(PWideChar(APath + '\libvlccore.dll'));
+
   Result := LoadLibrary(PWideChar(APath + '\libvlccore.dll'));
   Result&nbsp;:= LoadLibrary(PWideChar(APath + '\libvlc.dll'));
+
   Result := LoadLibrary(PWideChar(APath + '\libvlc.dll'));
 
end;
 
end;
 +
 +
</syntaxhighlight>
 +
=== Get address of libvlc functions ===
 +
 +
This will get address of libvlc functions. Only neccessary functions loaded for this sample code, please refer to libvlc document if you need more functions
 +
<syntaxhighlight lang="delphi">
 
// -----------------------------------------------------------------------------
 
// -----------------------------------------------------------------------------
 
function GetAProcAddress(handle: integer; var addr: Pointer; procName: string; failedList: TStringList): integer;
 
function GetAProcAddress(handle: integer; var addr: Pointer; procName: string; failedList: TStringList): integer;
 
begin
 
begin
   addr&nbsp;:= GetProcAddress(handle, PWideChar(procName));
+
   addr := GetProcAddress(handle, PWideChar(procName));
   if Assigned(addr) then Result&nbsp;:= 0
+
   if Assigned(addr) then Result := 0
 
   else begin
 
   else begin
 
     if Assigned(failedList) then failedList.Add(procName);
 
     if Assigned(failedList) then failedList.Add(procName);
     Result&nbsp;:= -1;
+
     Result := -1;
 
   end;
 
   end;
 
end;
 
end;
Line 117: Line 136:
 
   GetAProcAddress(vlcHandle, @libvlc_media_new_path, 'libvlc_media_new_path', failedList);
 
   GetAProcAddress(vlcHandle, @libvlc_media_new_path, 'libvlc_media_new_path', failedList);
 
   // if all functions loaded, result is an empty list, otherwise result is a list of functions failed
 
   // if all functions loaded, result is an empty list, otherwise result is a list of functions failed
   Result&nbsp;:= failedList.Count = 0;
+
   Result := failedList.Count = 0;
 +
end;
 +
 
 +
</syntaxhighlight>
 +
=== Create form  ===
 +
 
 +
Load library on form create. If you use a incompartible libvlc version (older or newer than this demo), some functions may not be correct (obsolete or agruments changed). In this case, program will display a list of functions failed to load, and you must rewrite these function prototype according to your libvlc version.
 +
<syntaxhighlight lang="delphi">
 +
// -----------------------------------------------------------------------------
 +
procedure TForm1.FormCreate(Sender: TObject);
 +
var sL: TStringList;
 +
begin
 +
  // load vlc library
 +
  vlclib := LoadVLCLibrary(GetVLCLibPath());
 +
  if vlclib = 0 then begin
 +
    Showmessage('Load vlc library failed');
 +
    Exit;
 +
  end;
 +
  // sL will contains list of functions fail to load
 +
  sL := TStringList.Create;
 +
  if not LoadVLCFunctions(vlclib, sL) then begin
 +
    Showmessage('Some functions failed to load : ' + #13#10 + sL.Text);
 +
    FreeLibrary(vlclib);
 +
    sL.Free;
 +
    Exit;
 +
  end;
 +
  sL.Free;
 +
end;
 +
 
 +
</syntaxhighlight>
 +
 
 +
=== Close form ===
 +
 
 +
Remember to unload libvlc library when exit
 +
<syntaxhighlight lang="delphi">
 +
// -----------------------------------------------------------------------------
 +
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
 +
begin
 +
  // unload vlc library
 +
  FreeLibrary(vlclib);
 
end;
 
end;
 +
</syntaxhighlight>
 +
=== Play a media<br> ===
 +
 +
Play a media (file or MURL) when user clicks on button "Play"
 +
<syntaxhighlight lang="delphi">
 
// -----------------------------------------------------------------------------
 
// -----------------------------------------------------------------------------
 
procedure TForm1.btnPlayClick(Sender: TObject);
 
procedure TForm1.btnPlayClick(Sender: TObject);
 
begin
 
begin
 
   // create new vlc instance
 
   // create new vlc instance
   vlcInstance&nbsp;:= libvlc_new(0, nil);
+
   vlcInstance := libvlc_new(0, nil);
 
 
 
   // create new vlc media from file
 
   // create new vlc media from file
   vlcMedia&nbsp;:= libvlc_media_new_path(vlcInstance, 'e:\udp\239.10.10.9.ts');
+
   vlcMedia := libvlc_media_new_path(vlcInstance, 'e:\udp\239.10.10.9.ts');
  
 
   // if you want to play from network, use libvlc_media_new_location instead
 
   // if you want to play from network, use libvlc_media_new_location instead
   // vlcMedia&nbsp;:= libvlc_media_new_location(vlcInstance, 'udp://@225.2.1.27:5127');
+
   // vlcMedia := libvlc_media_new_location(vlcInstance, 'udp://@225.2.1.27:5127');
  
 
   // create new vlc media player
 
   // create new vlc media player
   vlcMediaPlayer&nbsp;:= libvlc_media_player_new_from_media(vlcMedia);
+
   vlcMediaPlayer := libvlc_media_player_new_from_media(vlcMedia);
  
 
   // now no need the vlc media, free it
 
   // now no need the vlc media, free it
Line 142: Line 204:
 
   // play media
 
   // play media
 
   libvlc_media_player_play(vlcMediaPlayer);
 
   libvlc_media_player_play(vlcMediaPlayer);
end;
+
end;  
 +
</syntaxhighlight>
 +
=== Stop a media ===
 +
 
 +
And stop playing when user clicks on button "Stop"
 +
<syntaxhighlight lang="delphi">
 
// -----------------------------------------------------------------------------
 
// -----------------------------------------------------------------------------
 
procedure TForm1.btnStopClick(Sender: TObject);
 
procedure TForm1.btnStopClick(Sender: TObject);
Line 150: Line 217:
 
     Exit;
 
     Exit;
 
   end;
 
   end;
 
 
   // stop vlc media player
 
   // stop vlc media player
 
   libvlc_media_player_stop(vlcMediaPlayer);
 
   libvlc_media_player_stop(vlcMediaPlayer);
Line 159: Line 225:
 
   // release vlc media player
 
   // release vlc media player
 
   libvlc_media_player_release(vlcMediaPlayer);
 
   libvlc_media_player_release(vlcMediaPlayer);
   vlcMediaPlayer&nbsp;:= nil;
+
   vlcMediaPlayer := nil;
  
 
   // release vlc instance
 
   // release vlc instance
 
   libvlc_release(vlcInstance);
 
   libvlc_release(vlcInstance);
 
end;
 
end;
// -----------------------------------------------------------------------------
 
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
 
begin
 
  // unload vlc library
 
  FreeLibrary(vlclib);
 
end;
 
// -----------------------------------------------------------------------------
 
procedure TForm1.FormCreate(Sender: TObject);
 
var sL: TStringList;
 
begin
 
  // load vlc library
 
  vlclib&nbsp;:= LoadVLCLibrary(GetVLCLibPath());
 
  if vlclib = 0 then begin
 
    Showmessage('Load vlc library failed');
 
    Exit;
 
  end;
 
  
  // sL will contains list of functions fail to load
+
</syntaxhighlight>
  sL&nbsp;:= TStringList.Create;
+
 
  if not LoadVLCFunctions(vlclib, sL) then begin
+
[[Category:Coding]]
    Showmessage('Some functions failed to load&nbsp;: ' + #13#10 + sL.Text);
+
[[Category:libVLC]]
    FreeLibrary(vlclib);
 
    sL.Free;
 
    Exit;
 
  end;
 
  sL.Free;
 
end;
 
// -----------------------------------------------------------------------------
 
end.
 
</pre>  
 
[[Category:Libvlc]][[Category:Coding]][[Category:Delphi]]
 

Latest revision as of 09:06, 19 March 2019

See also: ActiveX/Delphi
This page contains example code for LibVLC.

This is a sample code to play media files using libVLC with Delphi (test with Delphi XE2 VCL form, VLC 1.1.11).

Form design

Use a simple form with 3 control : Panel1 (TPanel) to display video, btnPlay (TButton) to play media, and btnStop (TButton) to stop playing

Full source code

Implementations

unit Unit1;
interface

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

type
  TForm1 = class(TForm)
    btnPlay: TButton;
    btnStop: TButton;
    Panel1: TPanel;
    procedure btnPlayClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  plibvlc_instance_t        = type Pointer;
  plibvlc_media_player_t    = type Pointer;
  plibvlc_media_t           = type Pointer;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  libvlc_media_new_path              : function(p_instance : Plibvlc_instance_t; path : PAnsiChar) : Plibvlc_media_t; cdecl;
  libvlc_media_new_location          : function(p_instance : plibvlc_instance_t; psz_mrl : PAnsiChar) : Plibvlc_media_t; cdecl;
  libvlc_media_player_new_from_media : function(p_media : Plibvlc_media_t) : Plibvlc_media_player_t; cdecl;
  libvlc_media_player_set_hwnd       : procedure(p_media_player : Plibvlc_media_player_t; drawable : Pointer); cdecl;
  libvlc_media_player_play           : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
  libvlc_media_player_stop           : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
  libvlc_media_player_release        : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
  libvlc_media_player_is_playing     : function(p_media_player : Plibvlc_media_player_t) : Integer; cdecl;
  libvlc_media_release               : procedure(p_media : Plibvlc_media_t); cdecl;
  libvlc_new                         : function(argc : Integer; argv : PAnsiChar) : Plibvlc_instance_t; cdecl;
  libvlc_release                     : procedure(p_instance : Plibvlc_instance_t); cdecl;

  vlcLib: integer;
  vlcInstance: plibvlc_instance_t;
  vlcMedia: plibvlc_media_t;
  vlcMediaPlayer: plibvlc_media_player_t;

Get VLC installation path

Normally, the library libvlc.dll is in "C:\Program files\Videolan\VLC\". This function read registry to get the correct VLC installation path, stored in key HKEY_LOCAL_MACHINE\Software\VideoLAN\VLC.

// -----------------------------------------------------------------------------
// Read registry to get VLC installation path
// -----------------------------------------------------------------------------
function GetVLCLibPath: String;
var
  Handle: HKEY;
  RegType: Integer;
  DataSize: Cardinal;
  Key: PWideChar;
begin
  Result := '';
  Key := 'Software\VideoLAN\VLC';
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, Handle) = ERROR_SUCCESS then
  begin
    if RegQueryValueEx(Handle, 'InstallDir', nil, @RegType, nil, @DataSize) = ERROR_SUCCESS then
    begin
      SetLength(Result, DataSize);
      RegQueryValueEx(Handle, 'InstallDir', nil, @RegType, PByte(@Result[1]), @DataSize);
      Result[DataSize] := '\';
    end
    else Showmessage('Error on reading registry');
    RegCloseKey(Handle);
    Result := String(PChar(Result));
  end;
end;

Load libvlc library into memory

Next, load library libvlc.dll into memory

// -----------------------------------------------------------------------------
// Load libvlc library into memory
// -----------------------------------------------------------------------------
function LoadVLCLibrary(APath: string): integer;
begin
  Result := LoadLibrary(PWideChar(APath + '\libvlccore.dll'));
  Result := LoadLibrary(PWideChar(APath + '\libvlc.dll'));
end;

Get address of libvlc functions

This will get address of libvlc functions. Only neccessary functions loaded for this sample code, please refer to libvlc document if you need more functions

// -----------------------------------------------------------------------------
function GetAProcAddress(handle: integer; var addr: Pointer; procName: string; failedList: TStringList): integer;
begin
  addr := GetProcAddress(handle, PWideChar(procName));
  if Assigned(addr) then Result := 0
  else begin
    if Assigned(failedList) then failedList.Add(procName);
    Result := -1;
  end;
end;
// -----------------------------------------------------------------------------
// Get address of libvlc functions
// -----------------------------------------------------------------------------
function LoadVLCFunctions(vlcHandle: integer; failedList: TStringList): Boolean;
begin
  GetAProcAddress(vlcHandle, @libvlc_new, 'libvlc_new', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_new_location, 'libvlc_media_new_location', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_new_from_media, 'libvlc_media_player_new_from_media', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_release, 'libvlc_media_release', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_set_hwnd, 'libvlc_media_player_set_hwnd', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_play, 'libvlc_media_player_play', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_stop, 'libvlc_media_player_stop', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_release, 'libvlc_media_player_release', failedList);
  GetAProcAddress(vlcHandle, @libvlc_release, 'libvlc_release', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_player_is_playing, 'libvlc_media_player_is_playing', failedList);
  GetAProcAddress(vlcHandle, @libvlc_media_new_path, 'libvlc_media_new_path', failedList);
  // if all functions loaded, result is an empty list, otherwise result is a list of functions failed
  Result := failedList.Count = 0;
end;

Create form

Load library on form create. If you use a incompartible libvlc version (older or newer than this demo), some functions may not be correct (obsolete or agruments changed). In this case, program will display a list of functions failed to load, and you must rewrite these function prototype according to your libvlc version.

// -----------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var sL: TStringList;
begin
  // load vlc library
  vlclib := LoadVLCLibrary(GetVLCLibPath());
  if vlclib = 0 then begin
    Showmessage('Load vlc library failed');
    Exit;
  end;
  // sL will contains list of functions fail to load
  sL := TStringList.Create;
  if not LoadVLCFunctions(vlclib, sL) then begin
    Showmessage('Some functions failed to load : ' + #13#10 + sL.Text);
    FreeLibrary(vlclib);
    sL.Free;
    Exit;
  end;
  sL.Free;
end;

Close form

Remember to unload libvlc library when exit

// -----------------------------------------------------------------------------
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // unload vlc library
  FreeLibrary(vlclib);
end;

Play a media

Play a media (file or MURL) when user clicks on button "Play"

// -----------------------------------------------------------------------------
procedure TForm1.btnPlayClick(Sender: TObject);
begin
  // create new vlc instance
  vlcInstance := libvlc_new(0, nil);
  // create new vlc media from file
  vlcMedia := libvlc_media_new_path(vlcInstance, 'e:\udp\239.10.10.9.ts');

  // if you want to play from network, use libvlc_media_new_location instead
  // vlcMedia := libvlc_media_new_location(vlcInstance, 'udp://@225.2.1.27:5127');

  // create new vlc media player
  vlcMediaPlayer := libvlc_media_player_new_from_media(vlcMedia);

  // now no need the vlc media, free it
  libvlc_media_release(vlcMedia);

  // play video in a TPanel, if not call this routine, vlc media will open a new window
  libvlc_media_player_set_hwnd(vlcMediaPlayer, Pointer(Panel1.Handle));

  // play media
  libvlc_media_player_play(vlcMediaPlayer);
end;

Stop a media

And stop playing when user clicks on button "Stop"

// -----------------------------------------------------------------------------
procedure TForm1.btnStopClick(Sender: TObject);
begin
  if not Assigned(vlcMediaPlayer) then begin
    Showmessage('Not playing');
    Exit;
  end;
  // stop vlc media player
  libvlc_media_player_stop(vlcMediaPlayer);
  // and wait until it completely stops
  while libvlc_media_player_is_playing(vlcMediaPlayer) = 1 do begin
    Sleep(100);
  end;
  // release vlc media player
  libvlc_media_player_release(vlcMediaPlayer);
  vlcMediaPlayer := nil;

  // release vlc instance
  libvlc_release(vlcInstance);
end;