Using Memory Sources (Visual Basic)
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 Visual Basic. | |
2. |
Add the LEAD Multi-Media controls to your project. On the Project pull-down menu, use the Components option, and select the LEAD Multi-Media Library (14). | |
3. |
Select the ltmmPlayCtrl Control, and add it 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. |
Add the following to the declarations procedure of the general object in your main form: |
' Declare WIN32 API constants and functions in the Declarations module.
Private Const OF_READ = &H0
Private Const GMEM_MOVEABLE = &H2
Private Const SEEK_SET = 0
Private Const SEEK_END = 2
Private Type OFSTRUCT
cBytes As String * 1
fFixedDisk As String * 1
nErrCode As Integer
reserved As String * 4
szPathName As String * 128
End Type
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function llseek Lib "kernel32" Alias "_llseek" (ByVal hFile As Long, ByVal lOffset As Long, ByVal iOrigin As Long) As Long
Private Declare Function hread Lib "kernel32" Alias "_hread" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal wBytes As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
6. |
Handle the Form UnLoad event, and code Form_Unload sub as follows: |
Private Sub Form_Unload(Cancel As Integer)
Dim SourceHGlobal As Long
' Get the currrent SourceHGlobal property value.
SourceHGlobal = ltmmPlayCtrl1.SourceHGlobal
' Check if resources already allocated.
If (SourceHGlobal <> 0) Then
ltmmPlayCtrl1.ResetSource
GlobalFree (SourceHGlobal)
End If
End Sub
7. |
Handle the btnSetSourceHGlobal Click event, and code btnSetSourceHGlobal_Click sub as follows: |
Private Sub btnSetSourceHGlobal_Click()
Dim hf As Long ' For WIN32 declare this as Long
Dim SourceHGlobal As Long ' For WIN32 declare this as Long
Dim lpMem As Long
Dim uTotalFileSize As Long
Dim of As OFSTRUCT
' Get the currrent SourceHGlobal property value.
SourceHGlobal = ltmmPlayCtrl1.SourceHGlobal
' Check if resources already allocated.
If (SourceHGlobal <> 0) Then
' Reset the resources to free it.
ltmmPlayCtrl1.ResetSource
' Free the resource.
GlobalFree SourceHGlobal
' assign it to 0.
ltmmPlayCtrl1.SourceHGlobal = -1
End If
hf = OpenFile("C:\Source.avi", of, OF_READ)
If hf = -1 Then
MsgBox ("Error opening file!")
Exit Sub
End If
' Get the total file size
uTotalFileSize = llseek(hf, 0, SEEK_END)
llseek hf, 0, SEEK_SET
' allocate same-sized global memory
SourceHGlobal = GlobalAlloc(GMEM_MOVEABLE, uTotalFileSize)
' There is not enough memory!.
If SourceHGlobal = -1 Then
lclose (hf)
MsgBox ("Not enough memory to hold the file in memory!")
Exit Sub
End If
' lock our global data.
lpMem = GlobalLock(SourceHGlobal)
If lpMem = 0 Then
lclose (hf)
GlobalFree (SourceHGlobal)
MsgBox ("Not enough memory to hold the file in memory!")
Exit Sub
End If
If hread(hf, lpMem, uTotalFileSize) < uTotalFileSize Then
lclose (hf)
GlobalUnlock (SourceHGlobal)
GlobalFree (SourceHGlobal)
MsgBox ("Error reading file")
Exit Sub
End If
' close file
lclose (hf)
' Unlock our global data.
GlobalUnlock (SourceHGlobal)
' ASssign the SourceHGlobal Property to file data!.
ltmmPlayCtrl1.SourceHGlobal = SourceHGlobal
' Run this file
ltmmPlayCtrl1.Run
End Sub
8. |
Run your program to test it. |