Microsoft DirectX 8.1 (Visual Basic)

Play Buffer Notification

When streaming audio, you may want your application to be notified when the play cursor reaches a certain point in the buffer, or when playback is stopped. By using the DirectSoundSecondaryBuffer8.SetNotificationPositions method, you can set any number of points within the buffer where events are to be signaled. You cannot do this while the buffer is playing.

To set notification events on a buffer, you must first implement the DirectXEvent8 class in the form that contains the code for the buffer. Create a DirectXEvent8.DXCallback method by choosing DirectXEvent8 from the drop-down class list, and add the code you want to be executed when the event is signaled.

Create an event handle by calling the DirectX8.CreateEvent method and pass the event handle in the hEventNotify member of a DSBPOSITIONNOTIFY type. Pass the type to the DirectSoundSecondaryBuffer8.SetNotificationPositions method. The event handle will now be passed to DirectXEvent8.DXCallback whenever the play cursor reaches that position.

If you are taking advantage of voice management, it's possible that a buffer could be terminated before a notification position is reached. See the Remarks for DSBPOSITIONNOTIFY.

The following example code shows how to set up a single notification position. The following are the relevant lines in the Declarations section of the module:

Implements DirectXEvent8
 
Dim gDX As New DirectX8
Dim gDSB As DirectSoundSecondaryBuffer8
Dim endEvent As Long
Dim dsbpn(0) As DSBPOSITIONNOTIFY

Note that dsbpn must be an array, even though it contains only a single notification position.

In the Form_Load subroutine, an event is created:

endEvent = gDX.CreateEvent(Me)

After the DirectSoundSecondaryBuffer8 object has been created, the notification position is set and associated with the event, as follows:

With dsbpbn(0)
  .hEventNotify = endEvent
  .lOffset = DSBPN_OFFSETSTOP
End With
gDSB.SetNotificationPositions 1, dsbpn()

The offset of the notification, DSBPN_OFFSETSTOP or -1, is a special value indicating that the event is to be set when the buffer stops playing, either because it was not looping and the end of the buffer has been reached, or because the application called the DirectSoundSecondaryBuffer8.Stop method. When this happens, the application-defined callback function is automatically called, with the event identifier endEvent being passed in as the parameter.

Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  If eventid = endEvent Then
  lblStatus.Caption = "Buffer stopped."
  End If
End Sub