DirectX SDK |
When streaming audio, you may want your application to be notified when the current play position 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 by using 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, (LPVOID *)&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.
Sound buffers created with the DSBCAPS_CTRLPOSITIONNOTIFY flag must set a notification event. If you create a sound buffer with the NOTIFY flag, but don't actually set any notifications, the behavior is undefined. You may experience sounds being played back twice. Therefore, applications should not use DSBCAPS_CTRLPOSITIONNOTIFY unless they actually set a notification event.
When using LOC_DEFER and NOTIFY along with any voice management TERMINATEBY flag, it is possible that a sound that has notifications set, but not yet reached, may be terminated by the voice manager. In this event, the notification event will not occur.
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);
When streaming audio, you may want your application to be notified when the current play position reaches a certain point in the buffer, or when playback is stopped. With the DirectSoundBuffer.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 a notification event on a buffer, you must first implement a DirectXEvent object in the form that contains the code for the buffer. Then you create an event handle by calling the DirectX7.CreateEvent method and pass the event handle in the hEventNotify member of a DSBPOSITIONNOTIFY type which is a parameter of the DirectSoundBuffer.SetNotificationPositions method. When the play position reaches a notification position in the buffer, the DirectXEvent.DXCallback is called.
Note Sound buffers created with the DSBCAPS_CTRLPOSITIONNOTIFY flag must set a notification event. If you create a sound buffer with the NOTIFY flag, but don't actually set any notifications, the behavior is undefined. You may experience sounds being played back twice. Therefore, applications should not use DSBCAPS_CTRLPOSITIONNOTIFY unless they actually set a notification event.