Events for Visual Basic 6 COM interoperability |
|
Visual Basic 6 results unreliable when dealing with delegates and callbacks in general so, when using the API in conjunctions with this development environment, a set of COM compatible events is provided.
In order to enable the usage of the provided events you need to invoke the COMEventEnable method, for each type of event you are going to manage, immediately after initializing the API through the InitRecordingSystem method, then you need to manually enter the event management routine.
Inside the snippet below you can see an example of management of events generated by the recorder through the OnRecorderEvents event handler when a recording session is started and stopped and through the OnVuMeterValueChange event when VU Meter values are changed:
Visual Basic 6 |
' declare the API Public WithEvents m_SoundRecorderApi As AudioSoundRecorderApi.AudioSoundRecorderApiObj
Private Sub m_SoundRecorderApi_OnRecorderEvents(ByVal nEvent As enumRecorderEvents, _ ByVal nData1 As Long, ByVal nData2 As Long, _ ByVal nDataHigh3 As Long, ByVal nDataLow3 As Long, _ ByVal nDataHigh4 As Long, ByVal nDataLow4 As Long _ ) Select Case nEvent Case enumRecorderEvents.enumRecorderEvents_EV_REC_START: Debug.Print "Recording started" Case enumRecorderEvents.enumRecorderEvents_EV_REC_STOP: If nData1 = 1 Then MsgBox "Recording session completed" Else MsgBox "Recording session failed due to error " & m_SoundRecorderApi.LastError End If End Select End Sub
Private Sub m_SoundRecorderApi_OnVuMeterValueChange(ByVal nPeakLeft As Integer, ByVal nPeakRight As Integer) ' draw VU-Meter's bands using the graphic bars m_SoundRecorderApi.GraphicBarsManagerGet().SetValue m_hWndVuMeterLeft, nPeakLeft m_SoundRecorderApi.GraphicBarsManagerGet().SetValue m_hWndVuMeterRight, nPeakRight End Sub
Private Sub Form_Load() ' instance the API Set m_SoundRecorderApi = New AudioSoundRecorderApi.AudioSoundRecorderApiObj
' init the control m_SoundRecorderApi.InitRecordingSystem
' enable recorder and VU Meter events for COM clients like VB6 m_SoundRecorderApi.COMEventEnable enumEventTypesRec_EVENT_TYPE_RECORDER m_SoundRecorderApi.COMEventEnable enumEventTypesRec_EVENT_TYPE_REC_VU_METER
' start a recording session from the system default recording device m_audioRecorderAPI.StartFromDirectSoundDevice (0, 0, "c:\myrecording.mp3")
... do other stuffs
End Sub
|
as seen for regular ActiveX controls, the name of the event management routine is made of a number of parts: the name of the component (in this case "m_SoundRecorderApi" which was originally declared with the "WithEvents" attribute) followed by the underscore "_" character and by the name of the event (in this case "OnRecorderEvents") and related parameters.