Using Memory Sources (Delphi 6.0)
The ltmmConvertCtrl and ltmmPlayCtrl objects allow the user to supply media data through memory instead of a file. The source can be defined by a global memory handle or by an array.
1. |
Start Delphi 6. | |
2. |
If you didn’t install LEAD Multi-Media controls before, install them. On the Component pull-down menu, use the Import ActiveX Control… and select the LEAD Multi-Media Library (14), and Press install, and then compile and install the package dclusr.dpk. | |
3. |
From the ActiveX controls tab; add the ltmmPlayCtrl to your form. Size and position the control, as you want it to appear at run time. | |
4. |
Add button control to your form, and name it as follows: | |
|
Name |
Caption |
|
btnSetSourceHGlobal |
Set SourceHGlobal Property |
5. |
Handle the Form1 OnClose event, and code FormClose procedure as follows: |
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
SourceHGlobal: HGLOBAL;
begin
// Get the currrent SourceHGlobal property value.
SourceHGlobal:= ltmmPlayCtrl1.SourceHGlobal;
// Check if resources are already allocated.
if ( SourceHGlobal <> 0 ) then
begin
ltmmPlayCtrl1.ResetSource ( );
GlobalFree ( SourceHGlobal ) ;
end;
end;
6. |
Handle the btnSetSourceHGlobal OnClick event, and code btnSetSourceHGlobalClick procedure as follows: |
procedure TForm1.btnSetSourceHGlobalClick(Sender: TObject);
var
buffer: PChar;
FileHandle: HFile;
SourceHGlobal: HGLOBAL;
uBytesLeft: Cardinal;
uBytesToRead: Cardinal;
uTotalFileSize: Cardinal;
begin
// Get the currrent SourceHGlobal property value.
SourceHGlobal:= ltmmPlayCtrl1.SourceHGlobal;
// Check if resources are already allocated.
if ( SourceHGlobal <> 0 ) then
begin
// Reset the resources to free it.
ltmmPlayCtrl1.ResetSource ( );
// Free the resource.
GlobalFree ( SourceHGlobal ) ;
// assign it to 0.
ltmmPlayCtrl1.SourceHGlobal:= 0;
end;
// Open out source avi file.
FileHandle := _lopen ( 'C:\Source.avi', OF_READ );
// check if it is opened correctly!.
if ( FileHandle <> HFILE_ERROR ) then
begin {valid file handle}
// We will read 60 KB Chuck at time.
uBytesToRead:= 60 * 1024;
// Get the total file size
uTotalFileSize:= GetFileSize ( FileHandle, Nil);
// allocate same-sized global memory
SourceHGlobal:= GlobalAlloc ( GMEM_MOVEABLE, uTotalFileSize );
// There is not enough memory!.
if( SourceHGlobal = 0 ) then
begin
_lClose ( FileHandle );
Exit;
end;
// lock our global data.
buffer:= GlobalLock( SourceHGlobal );
// the bytes to read = totlal file size!.
uBytesLeft:= uTotalFileSize;
// Loop until we read all the file data.
while ( uBytesLeft > 0 ) do
begin
if ( uBytesLeft < uBytesToRead ) then
begin
_lread ( FileHandle, buffer, uBytesLeft );
break;
end
else
begin
_lread ( FileHandle, buffer, uBytesToRead );
end;
buffer:= buffer + uBytesToRead;
uBytesLeft:= uBytesLeft - uBytesToRead;
end;
// Unlock our global data.
GlobalUnlock( SourceHGlobal );
// close file
_lclose ( FileHandle );
// ASssign the SourceHGlobal Property to file data!.
ltmmPlayCtrl1.SourceHGlobal:= SourceHGlobal;
// Run this file
ltmmPlayCtrl1.Run ( );
end;
end;
7. |
Run your program to test it. |