Microsoft DirectX 8.1 (C++)

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. With the IDirectSoundNotify8::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.

First you have to obtain a pointer to the IDirectSoundNotify8 interface. You can do this by using the buffer object's QueryInterface method, as in the following example, where lpDsbSecondary is an IDirectSoundBuffer8 interface pointer:

LPDIRECTSOUNDNOTIFY8 lpDsNotify; 
 
HRESULT hr = lpDsbSecondary->QueryInterface(IID_IDirectSoundNotify8, 
                (LPVOID *)&lpDsNotify); 
if SUCCEEDED(hr) 
{ 
  // Go ahead and use lpDsNotify->SetNotificationPositions. 
} 
 

The IDirectSoundNotify8 interface is associated with the buffer object from which you obtained the pointer. The methods of the interface automatically apply to that buffer.

Now create an event object with the Win32 CreateEvent function. Put the handle to this event in the hEventNotify member of a DSBPOSITIONNOTIFY structure. In the dwOffset member of that structure, specify the offset within the buffer where you want the event to be signaled. Then pass the address of the structure—or of an array of structures, if you want to set more than one notification position—to the IDirectSoundNotify8::SetNotificationPositions method.

The following example sets a single notification position. The event will be signaled when playback stops, either because it was not looping and the end of the buffer has been reached, or because the application called the IDirectSoundBuffer8::Stop method.

DSBPOSITIONNOTIFY PositionNotify;
 
PositionNotify.Offset = DSBPN_OFFSETSTOP;
PositionNotify.hEventNotify = hMyEvent;
// hMyEvent is the handle returned by CreateEvent().
 
lpDsNotify->SetNotificationPositions(1, &PositionNotify);
 

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.