Checking if wave file is playing

I use waveOutWrite() to play segments of a large wave file in Visual C++.  After I start playing it, I need to know whether the wave segment finished playing or not.
What is a way to check this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

waveOutGetPosition( ) ??
mroslyakAuthor Commented:
I tried using waveOutGetPosition() but was not able to get it to work, since I could not find a lot of documentaion on it
A simple example would be nice.
Have you tried looking for the MM_WOM_DONE message? This is how I determine when a segment is complete and when to move to the bext one. Here is an explanation of it:

The MM_WOM_DONE message is sent to a window when the given output buffer is being returned to the application. Buffers are returned to the application when they have been played, or as the result of a call to the waveOutReset function.

wParam = (WPARAM) hOutputDev
lParam = (LONG) lpwvhdr
Handle of the waveform-audio output device that played the buffer.

Address of a WAVEHDR structure identifying the buffer.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

WDB is right. You may find the following article useful.

Recording and Playing Waveform Audio
When u open the device with waveOutOpen , put the CALLBACK_WINDOW flag and a handle to a window . When writing to the device finishes , your window receives a message MM_WOM_DONE .
After a successful call to:

   waveOutWrite(..., ph, ...);

just check ph->dwFlags. When the wave buffer is finished playing, WHDR_DONE will be set in dwFlags. So:

   if (WHDR_DONE ==
   { //finished playing

   } else
   { //not finished playing

You are supposed to keep *ph around so you can call waveOutUnprepareHeader() later.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Checking the ph->dwFLags is not as convienent as waiting for the MM_WOM_DONE message. When you check the ph->dwFlags and WHDR_DONE isn't set then you need to keep checking until it is. Therefore, this method will require some extra logic, not to mention it won't be as efficient as processing MM_WOM_DONE.
You don't have to check it in an endless loop, just once in a while. If at any given time you want to check if a wave block finished playing, I'd say WHDR_DONE is the way to go, even if you have a window callback for other purposes already.

If you wait for MM_WOM_DONE, where are you going to put the finished flags for wave blocks ? Some extra variables? You might as well just check dwFlags as the systems sets it for you.
Why would I need to put the finished flags anywhere?? I still have ph and the flags still get set but I won't need to do any extra work in determining when they get set.
   I've worked with many MM apps and  MM_WOM_DONE is always used. I can think of some special instances where it would be useful to keep tabs on the flag, but just to find out when a block has finished, MM_WOM_DONE is the way to go.
Sorry, I have to disagree there. What I mean is, for example, 1 second after a wave started playing, you need to check if it's finished. Using MM_WOM_DONE, how would you do it ? At minimum you need a one-bit flag to indicate if MM_WOM_DONE is sent already. So why not just check dwFlags in wave header, which you already have anyway.

Besides, checking the WHR_DONE is simpler as it doesn't require you to worry about windows message callback.

Maybe I'm missing something here? The only way that method makes sense to me is if you are planning on doing something different to the buffer if it hasn't finished playing after 1 second, other then allowing it to continue to play.    
 Otherwise, why would you want to poll for this when the windows message callback will tell you when it's done? To me this seems very efficent and straight forward.
   Also, when polling the flag, you will have to have your timing just right, otherwise your application could take a performance hit or even experience audio dropouts when playing wave files. So, additional coding would be neccessary to make sure the timing is right.
   And, what is wrong with a windows message callback? They are very easy to setup, efficient to use, not to mention they lend themselves very well to most MM components.
  So, my point is, an application that plays a wave file and uses MM_WOM_DONE to determine and set the "buffer flow" will be more efficient and easier to code then an application that attempts to poll the wave header flags. I've yet to see it done any other way.

Take Care :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.