我正在使用DirectSound在C ++中播放音乐。我正在使用DSBPOSITIONNOTIFICATION变量来确定secondaryBuffer
变量(类型IDirectSoundBuffer8*
)的播放何时达到四分之一,四分之三或其大小的结尾。用于创建通知和播放音乐的代码如下所示。
LPDIRECTSOUNDNOTIFY8 directSoundNotify;
HANDLE playEventHandles[3];
playEventHandles[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
playEventHandles[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
playEventHandles[2] = CreateEvent(NULL, FALSE, FALSE, NULL);
secondaryBuffer->QueryInterface(IID_IDirectSoundNotify8, (LPVOID*)&directSoundNotify);
//This is the size of secondaryBuffer
int averageBytes = wfx.Format.nAvgBytesPerSec * 4;
//Create notification information
DSBPOSITIONNOTIFY positionNotify[3];
positionNotify[0].dwOffset = averageBytes / 4;
positionNotify[0].hEventNotify = playEventHandles[0];
positionNotify[1].dwOffset = averageBytes - (averageBytes / 4);
positionNotify[1].hEventNotify = playEventHandles[1];
positionNotify[2].dwOffset = averageBytes;
positionNotify[2].hEventNotify = playEventHandles[2];
directSoundNotify->SetNotificationPositions(1, positionNotify);
directSoundNotify->Release();
secondaryBuffer->Play(0, 0, 0);
do
{
//Wait for notifications
DWORD notification = WaitForMultipleObjects(3, playEventHandles, FALSE, INFINITE);
//First quarter notification - Fill second half if not all data has been received
if (notification == 0)
{
//This function is handled elsewhere in the program - It obtains music from a server using sockets
ReceiveMusic();
}
//Third quarter notification - Fill first half if not all data has been received
else if (notification == 1)
{
ReceiveMusic();
}
//Playback ended notification - Reset play position
else if (notification == 2)
{
secondaryBuffer->SetCurrentPosition(0);
secondaryBuffer->Play(0, 0, 0);
}
} while (size < dataBufferSize); //While size (current amount of song loaded) is less than dataBufferSize (total size of song that is in the process of being loaded)
//The size and dataBufferSize are initialised and handled elsewhere in the program, but they work properly - There shouldn't be a problem with the loop termination condition
函数WaitForMultipleObjects()
能够检测事件发生的时间(它成功通知第一季度事件)。但是,它停止在第二个循环上工作。该程序到达该功能,但它只是永远坐在那里而没有注意到另一个事件发生时(我很确定secondaryBuffer
仍在播放,所以应该注意到它已经达到四分之三回放或已经停止) 。如何更改代码以便函数实际注意到未来循环中的事件?