My Third Capture (Visual Basic)
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. |
Choose Tools –> Menu Editor menu item. |
3. |
Add 1 Main menu items and 1 sub menu item to this main menu item and name them as follows. Note that menuVideoDevice is the menu item, and menuVideoDeviceAll is the sub menu item of it. |
Name |
Caption |
Index |
menuVideoDevice |
&Video Device |
Don’t fill it. |
menuVideoDeviceAll |
&None |
0 |
4. |
Add another 1 Main menu items and another 1 sub menu item to this main menu item and name them as follows. Note that menuAudioDevice is the menu item, and menuAudioDeviceAll is the sub menu item of it. |
Name |
Caption |
Index |
menuAudioDevice |
&Audio Device |
Don’t fill it. |
menuAudioDeviceAll |
&None |
0 |
5. |
Update the Form_Load Sub to be as follows: |
Private Sub Form_Load()
'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
' update the menu items
UpdateMenuStatus
End Sub
6. |
Update the btnStartCapture_Click to be as follows: |
Private Sub btnStartCapture_Click()
ltmmCaptureCtrl1.StartCapture ltmmCapture_Mode_VideoOrAudio
End Sub
7. |
Handle the menuVideoDeviceAll sub menu item Click event, and code menuVideoDeviceAll_Click Sub as follows: |
Private Sub menuVideoDeviceAll_Click(Index As Integer)
Dim devices As IltmmDevices
MousePointer = vbHourglass
Set devices = ltmmCaptureCtrl1.VideoDevices
devices.Selection = menuVideoDeviceAll(Index).Tag
MousePointer = crDefault
UpdateMenuStatus
End Sub
8. |
Handle the menuAudioDeviceAll sub menu item Click event, and code menuAudioDeviceAll_Click Sub as follows: |
Private Sub menuAudioDeviceAll_Click(Index As Integer)
Dim devices As IltmmDevices
MousePointer = vbHourglass
Set devices = ltmmCaptureCtrl1.AudioDevices
devices.Selection = menuAudioDeviceAll(Index).Tag
Cursor = crDefault
UpdateMenuStatus
End Sub
9. |
Add the body of BuildDeviceMenu, and UpdateMenuStatus Subs to your project. |
Sub BuildDeviceMenu()
Dim count As Integer
Dim i As Integer
Dim name As String
count = ltmmCaptureCtrl1.VideoDevices.count
' Adding the 'None' menu Item.
' Set the caption to 'None'.
menuVideoDeviceAll(0).Caption = "None"
' set the Tag to -1 to use it when we will set the Selection Property.
menuVideoDeviceAll(0).Tag = -1
If (count > 0) Then
' Setect the first device.
ltmmCaptureCtrl1.VideoDevices.Selection = 0
' Adding the Video Devices to the Video Device menu item.
For i = 0 To count - 1
'device:= devices.Item ( i )
' Get the Device name
name = ltmmCaptureCtrl1.VideoDevices.Item(i).FriendlyName
' Create the Menu Item.
Load menuVideoDeviceAll(i + 1)
' Assign the caption to the device name.
menuVideoDeviceAll(i + 1).Caption = name
' set the Tag to i to use it when we will set the Selection Property.
menuVideoDeviceAll(i + 1).Tag = i
Next
End If
count = ltmmCaptureCtrl1.AudioDevices.count
' Adding the 'None' menu Item.
' Set the caption to 'None'.
menuAudioDeviceAll(0).Caption = "None"
' set the Tag to -1 to use it when we will set the Selection Property.
menuAudioDeviceAll(0).Tag = -1
If (count > 0) Then
' Setect the first device.
ltmmCaptureCtrl1.AudioDevices.Selection = 0
' Adding the Video Devices to the Video Device menu item.
For i = 0 To count - 1
'device:= devices.Item ( i )
' Get the Device name
name = ltmmCaptureCtrl1.AudioDevices.Item(i).FriendlyName
' Create the Menu Item.
Load menuAudioDeviceAll(i + 1)
' Assign the caption to the device name.
menuAudioDeviceAll(i + 1).Caption = name
' set the Tag to i to use it when we will set the Selection Property.
menuAudioDeviceAll(i + 1).Tag = i
Next
End If
End Sub
Sub UpdateMenuStatus()
Dim i As Integer
Dim Index As Integer
Dim state As Boolean
Dim devices As ltmmDevices
state = (ltmmCaptureCtrl1.state <> ltmmCapture_State_Running)
' Video Devices menu item
Set devices = ltmmCaptureCtrl1.VideoDevices
Index = devices.Selection
For i = 0 To devices.count
menuVideoDeviceAll(i).Enabled = state
Next
For i = 0 To ltmmCaptureCtrl1.VideoDevices.count
If (menuVideoDeviceAll(i).Tag = Index) Then
menuVideoDeviceAll(i).Checked = True
Else
menuVideoDeviceAll(i).Checked = False
End If
Next
' Audio Devices menu item
Set devices = ltmmCaptureCtrl1.AudioDevices
Index = devices.Selection
For i = 0 To devices.count
menuAudioDeviceAll(i).Enabled = state
Next
For i = 0 To devices.count
If (menuAudioDeviceAll(i).Tag = Index) Then
menuAudioDeviceAll(i).Checked = True
Else
menuAudioDeviceAll(i).Checked = False
End If
Next
End Sub
10. |
Handle the ltmmCaptureCtrl1 Complete event, and code the ltmmCaptureCtrl1_Complete Sub as follows: |
Private Sub ltmmCaptureCtrl1_Complete ()
Caption = "Complete"
End Sub
11. |
Handle the ltmmCaptureCtrl1 ErrorAbort event, and code the ltmmCaptureCtrl1_ErrorAbort Sub as follows: |
Private Sub ltmmCaptureCtrl1_ErrorAbort(ByVal ErrorCode As Long)
Caption = "Aborted"
MsgBox "Capture Aborted. Error " + CStr(ErrorCode)
End Sub
12. |
Handle the ltmmCaptureCtrl1 Progress event, and code the ltmmCaptureCtrl1_Progress Sub as follows: |
Private Sub ltmmCaptureCtrl1_Progress(ByVal Time As Long)
Dim str1 As String
Dim str2 As String
Dim str3 As String
Dim str4 As String
str1 = "Dropped " + CStr(ltmmCaptureCtrl1.NumDropped)
str2 = "Not Dropped " + CStr(ltmmCaptureCtrl1.NumNotDropped)
str3 = "Frm. Size " + CStr(ltmmCaptureCtrl1.AverageFrameSize)
If ((ltmmCaptureCtrl1.Mode = ltmmCapture_Mode_Still) Or (ltmmCaptureCtrl1.Mode = ltmmCapture_Mode_ManualFrames)) Then
str4 = ""
Else
str4 = CStr(ltmmCaptureCtrl1.CaptureTime)
End If
Caption = str1 + " " + str2 + " " + str3 + " " + str4
End Sub
13. |
Handle the ltmmCaptureCtrl1 Started event, and code the ltmmCaptureCtrl1_Started Sub as follows: |
Private Sub ltmmCaptureCtrl1_Started ()
Caption = "Started"
End Sub
14. |
Run your program to test it. |