Why does WaitForMultipleObjects() stop working after one loop?

431 Views Asked by At

I am using DirectSound to play music in C++. I am using DSBPOSITIONNOTIFICATION variables to determine when playback of the secondaryBuffer variable (of type IDirectSoundBuffer8*) has reached either one quarter, three quarters or the end of its size. The code used to create the notifications and play the music is shown below.

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

The function WaitForMultipleObjects() is able to detect when an event has occurred (it successfully notices the first quarter event). However, it stops working on the second loop. The program reaches the function, but it just sits there forever without noticing when another event has occurred (I'm pretty sure the secondaryBuffer is still playing, so it should notice that it has reached three quarters playback or has stopped). How can I change the code so that the function actually notices the events on future loops?

0

There are 0 best solutions below