How do I determine when an audio file(.wav) is done playing on Apalis iMX6?

I have incorporated the sample audio program into a large program which will utilize the Apalis iMX6 SoM, I have successfully played two *.wav files while in debug build for WEC7/VS2008, but when compiled for Release, the *.wav files do not play unless a 5 second(arbitrarily chosen) delay is placed after the waveOutWrite() call followed by the waveOutSetVolume() call(with Volume set to max).
The large program utilizes a messaging system, so the .wav files are processed as they fill the queue.
Is there a preferred method to what I am anticipating as determining the longest time of one of the two .wav files, and waiting that amount?
Any Toradex suggestions with this(utilzing analog audio connector on Apalis iMX6 eval board)?

Did you check the return code of waveOutWrite when it fails? Does it return WAVERR_STILLPLAYING?
You may call it in a loop with a short Sleep (few ms) in between as long as it is returning that error or you may use sndPlaySound with the SND_SYNC flag to have the call returning when playback is actually finished.
If you know sound duration in advance, use Sleep in a high-priority thread (above app priority 250) to delay the playback of next sound for the right amount of time.

We addressed this issue with the following code block (inserted after call to waveOutWrite():

while (!(waveHeader.dwFlags & WHDR_DONE))

See “Remarks” section at this link: Microsoft Windows Dev Center, waveOutWrite function