My Third Capture (Delphi 6.0)
The ltmmCaptureCtrl object allows the user to capture data from video and audio hardware devices. Using the following steps:
1. |
Start with the project that you created in My First Capture. |
|
2. |
Add a Main Menu control to your form. |
|
3. |
Add two menu items to your main menu, and name them as follows: |
|
|
Name |
Caption |
|
menuVideoDevice |
&Video Device |
|
menuAudioDevice |
&Audio Device |
4. |
Add the following declarations to the private section of Form1. |
Procedure BuildDeviceMenu ( ) ;
Procedure menuVideoDeviceAllClick(Sender: TObject);
Procedure menuAudioDeviceAllClick(Sender: TObject);
5. |
Update the FormCreate procedure to be as follows: |
procedure TForm1.FormCreate(Sender: TObject);
begin
//use preview
ltmmCaptureCtrl1.Preview:= True;
//select first video device
ltmmCaptureCtrl1.VideoDevices.Selection:= 0;
// Set the Target File.
ltmmCaptureCtrl1.TargetFile:= 'c:\target.avi';
// Enumerate the Video\Audio devices.
BuildDeviceMenu ( ) ;
end;
6. |
Update the btnStartCaptureClick to be as follows: |
procedure TForm1.btnStartCaptureClick(Sender: TObject);
begin
ltmmCaptureCtrl1.StartCapture ( ltmmCapture_Mode_VideoOrAudio );
end;
7. |
Handle the menuVideoDevice menu item OnClick event, and code menuVideoDeviceClick procedure as follows: |
procedure TForm1.menuVideoDeviceClick(Sender: TObject);
var
i: Integer ;
index: Integer;
state: Boolean ;
devices: IltmmDevices ;
begin
state:= ( ltmmCaptureCtrl1.State <> ltmmCapture_State_Running ) ;
for i:= 0 to menuVideoDevice.Count - 1 do
begin
menuVideoDevice.Items [ i ].Enabled := state ;
end;
devices:= ltmmCaptureCtrl1.VideoDevices ;
index:= devices.Selection ;
for i:= 0 to menuVideoDevice.Count - 1 do
begin
if ( menuVideoDevice.Items [ i ].Tag = index ) then
menuVideoDevice.Items [ i ].Checked := True
else
menuVideoDevice.Items [ i ].Checked := False;
end;
end;
8. |
Handle the menuAudioDevice menu item OnClick event, and code menuAudioDeviceClick procedure as follows: |
procedure TForm1.menuAudioDeviceClick(Sender: TObject);
var
i: Integer ;
index: Integer;
state: Boolean ;
devices: IltmmDevices ;
begin
state := ( ltmmCaptureCtrl1.State <> ltmmCapture_State_Running ) ;
for i:= 0 to menuAudioDevice.Count - 1 do
begin
menuAudioDevice.Items [ i ].Enabled := state ;
end;
devices:= ltmmCaptureCtrl1.AudioDevices ;
index:= devices.Selection ;
for i:= 0 to menuAudioDevice.Count - 1 do
begin
if ( menuAudioDevice.Items [ i ].Tag = index ) then
menuAudioDevice.Items [ i ].Checked := True
else
menuAudioDevice.Items [ i ].Checked := False;
end;
end;
9. |
Add the body of BuildDeviceMenu, menuVideoDeviceAllClick, and menuAudioDeviceAllClick procedures to your Unit1 file. |
Procedure TForm1.BuildDeviceMenu ( ) ;
var
count, i: Integer;
devices: IltmmDevices;
device: IltmmDevice;
tmpMenuItem: TMenuItem;
name: String;
begin
// Removing the Old video devices menu Items
for i:= menuVideoDevice.Count - 1 downto 0 do
begin
menuVideoDevice.Remove ( menuVideoDevice.Items[i] );
end;
devices:= ltmmCaptureCtrl1.VideoDevices;
count := devices.Count;
// Adding the 'None' menu Item.
// Create the Menu Item.
tmpMenuItem := TMenuItem.Create ( Self ) ;
// Set the caption to 'None'.
tmpMenuItem.Caption := 'None' ;
// set the Tag to -1 to use it when we will set the Selection Property.
tmpMenuItem.Tag := -1 ;
// Add the method that will handle the OnClick event.
tmpMenuItem.OnClick := menuVideoDeviceAllClick ;
// Add the new menu Item.
menuVideoDevice.Add ( tmpMenuItem ) ;
if( count > 0 ) then
begin
// Setect the first device.
devices.Selection := 0 ;
// Adding the Video Devices to the Video Device menu item.
for i := 0 to count -1 do
begin
device:= devices.Item( i );
// Get the Device name
name:= device.FriendlyName;
// Create the Menu Item.
tmpMenuItem:= TMenuItem.Create ( Self ) ;
// Assign the caption to the device name.
tmpMenuItem.Caption := name ;
// set the Tag to i to use it when we will set the Selection Property.
tmpMenuItem.Tag := i ;
// Add the method that will handle the OnClick event.
tmpMenuItem.OnClick := menuVideoDeviceAllClick ;
// Add the new menu Item.
menuVideoDevice.Add ( tmpMenuItem ) ;
end;
end;
// Removing the Old Audio devices menu items
for i:= menuAudioDevice.Count - 1 downto 0 do
begin
menuAudioDevice.Remove ( menuAudioDevice.Items[i] );
end;
devices:= ltmmCaptureCtrl1.AudioDevices;
count:= devices.Count;
// Adding the 'None' menu Item.
// Create the Menu Item.
tmpMenuItem := TMenuItem.Create ( Self ) ;
// Set the caption to 'None'.
tmpMenuItem.Caption := 'None' ;
// set the Tag to -1 to use it when we will set the Selection Property.
tmpMenuItem.Tag := -1 ;
// Add the method that will handle the OnClick event.
tmpMenuItem.OnClick := menuAudioDeviceAllClick ;
// Add the new menu Item.
menuAudioDevice.Add ( tmpMenuItem ) ;
if ( count > 0 ) then
begin
// Setect the first device.
devices.Selection := 0;
// Adding the Audio Devices to the Video Device menu item.
for i := 0 to count - 1 do
begin
device:= devices.Item (i);
// Get the Device name.
name:= device.FriendlyName;
// Create the Menu Item.
tmpMenuItem:= TMenuItem.Create ( Self ) ;
// Assign the caption to the device name.
tmpMenuItem.Caption := name ;
// set the Tag to i to use it when we will set the Selection Property.
tmpMenuItem.Tag := i ;
// Add the method that will handle the OnClick event.
tmpMenuItem.OnClick := menuAudioDeviceAllClick ;
// Add the new menu Item.
menuAudioDevice.Add ( tmpMenuItem ) ;
end;
end;
end;
Procedure TForm1.menuVideoDeviceAllClick(Sender: TObject);
var
devices: IltmmDevices ;
begin
Cursor:= crHourGlass ;
devices:= ltmmCaptureCtrl1.VideoDevices ;
devices.Selection := TMenuItem ( Sender ).Tag ;
Cursor:= crDefault ;
end;
Procedure TForm1.menuAudioDeviceAllClick(Sender: TObject);
var
devices: IltmmDevices;
begin
Cursor:= crHourGlass ;
devices:= ltmmCaptureCtrl1.AudioDevices ;
devices.Selection := TMenuItem(Sender).Tag ;
Cursor:= crDefault;
end;
10. |
Handle the ltmmCaptureCtrl1 OnComplete event, and code the ltmmCaptureCtrl1Complete procedure as follows: |
procedure TForm1.ltmmCaptureCtrl1Complete(Sender: TObject);
begin
Caption:= 'Complete';
end;
11. |
Handle the ltmmCaptureCtrl1 OnErrorAbort event, and code the ltmmCaptureCtrl1ErrorAbort procedure as follows: |
procedure TForm1.ltmmCaptureCtrl1ErrorAbort(Sender: TObject;
ErrorCode: Integer);
begin
Caption:= 'Aborted';
ShowMessage ( 'Capture Aborted. Error ' + IntToStr(ErrorCode));
end;
12. |
Handle the ltmmCaptureCtrl1 OnProgress event, and code the ltmmCaptureCtrl1Progress procedure as follows: |
procedure TForm1.ltmmCaptureCtrl1Progress(Sender: TObject; Time: Integer);
var
str1: String;
str2: String;
str3: String;
str4: String;
begin
str1:= 'Dropped ' + IntToStr(ltmmCaptureCtrl1.NumDropped);
str2:= 'Not Dropped ' + IntToStr(ltmmCaptureCtrl1.NumNotDropped);
str3:= 'Frm. Size ' + IntToStr(ltmmCaptureCtrl1.AverageFrameSize);
if ((ltmmCaptureCtrl1.Mode = ltmmCapture_Mode_Still) Or (ltmmCaptureCtrl1.Mode = ltmmCapture_Mode_ManualFrames)) Then
str4:= ''
else
str4:= FloatToStr(ltmmCaptureCtrl1.CaptureTime);
Caption:= str1 + ' ' + str2 + ' ' + str3 + ' ' + str4;
end;
13. |
Handle the ltmmCaptureCtrl1 OnStarted event, and code the ltmmCaptureCtrl1Started procedure as follows: |
procedure TForm1.ltmmCaptureCtrl1Started(Sender: TObject);
begin
Caption:= 'Started';
end;
14. |
Run your program to test it. |