Particularly 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 IDirectSoundNotify::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 IDirectSoundNotify interface. You can do this with the buffer object's QueryInterface method, as in the following C++ example:
// LPDIRECTSOUNDBUFFER lpDsbSecondary;
// The buffer has been initialized already.
LPDIRECTSOUNDNOTIFY lpDsNotify; // pointer to the interface
HRESULT hr = lpDsbSecondary->QueryInterface(IID_IDirectSoundNotify,
&lpDsNotify);
if (SUCCEEDED(hr))
{
// Go ahead and use lpDsNotify->SetNotificationPositions.
}
Note The IDirectSoundNotify interface is associated with the object that obtained the pointer, in this case the secondary buffer. The methods of the new interface will automatically apply to that buffer.
Now create an event object with the Win32 CreateEvent function. You put the handle to this event in the hEventNotify member of a DSBPOSITIONNOTIFY structure, and in the dwOffset member of that structure you specify the offset within the buffer where you want the event to be signaled. Then you pass the address of the structure—or of an array of structures, if you want to set more than one notification position—to the IDirectSoundNotify::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 IDirectSoundBuffer::Stop method.
DSBPOSITIONNOTIFY PositionNotify;
PositionNotify.Offset = DSBPN_OFFSETSTOP;
PositionNotify.hEventNotify = hMyEvent;
// hMyEvent is the handle returned by CreateEvent()
lpDsNotify->SetNotificationPositions(1, &PositionNotify);