How to tell when a sound has finished playing ???

J

Jack

Hi,

I'm writing a simple wav player (like winamp) and using the
SoundPlayer class in c# 2005.

Using winform buttons, I can begin playing the sound and stop half-way
through the sound using:
myPlayer.Play() and myPlayer.Stop() methods which works great, but i
want to know when the sound actually stops playing (so I can change my
button's picture). If I raise a custom event after the myPlayer.Play()
command, the event is raised immediately after the sound starts, NOT
when the sound finishes playing. This makes sense to me because it is
playing the sound 'asynchronously'. This method does work if I raise a
custom event after the myPlayer.PlaySync() instead of myPlayer.Sync(),
but the .PlaySync method waits until the entire sound finishes
playing, thus disables everything on the form and the user cannot
click the 'Stop' button to call the myPlayer.Stop() method.

I've had a go at putting this into a thread, but the main problem is
that the thread doesn't abort immediately; unlike the myPlayer.Stop()
which does. (I'm pretty sure the reason for this is that the thread
management is handled by the CLR (obviously!) and that it wil abort
the thread when it's ready i.e. NOT immediately)

How on earth can one do this?
Any ideas greatly appreciated.
Jack.
 
J

Jack

[...]
I've had a go at putting this into a thread, but the main problem is
that the thread doesn't abort immediately; unlike the myPlayer.Stop()
which does. (I'm pretty sure the reason for this is that the thread
management is handled by the CLR (obviously!) and that it wil abort
the thread when it's ready i.e. NOT immediately)
How on earth can one do this?

The basic answer is that you need to play the sound in a thread.  You  
mention that you've already tried this and your complaint is that "the  
thread doesn't abort immediately".  I don't know why that's a problem.  
The whole point of that approach is that the thread will remain until the  
sound has stopped playing, without blocking your main GUI thread.  Why do  
you want the thread to be "aborted"?

Pete

I want the thread to be aborted as soon as the user clicks the 'Stop'
button. The reason for this is so the sound can stop playing as soon
as when the use clicks 'Stop'. (Like a standard CD player)

The problem is that when the 'Stop' button is pressed, I can tell the
system to abort the thread, but the system "doesn't actually abort the
thread" straight away. Even though i've "told" the system to abort the
thread, it's up to the CLR to abort it "when it wants to", and not
immediately. The result of this is that the sound actually plays to
the end.

The idea of mine was that at the end of normal termination of the
thread (i.e. when the sound finished playing) I can raise a custom
event. My program will then know when the sound has finished playing.

Any ideas?
Thanks, Jack.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top