471,338 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

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

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.
Jun 27 '08 #1
3 7334
On Mon, 02 Jun 2008 03:52:51 -0700, Jack <br*********@hotmail.comwrote:
[...]
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
Jun 27 '08 #2
On Jun 3, 1:23*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 02 Jun 2008 03:52:51 -0700, Jack <bradnerd...@hotmail.comwrote:
[...]
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.
Jun 27 '08 #3
On Wed, 04 Jun 2008 23:41:34 -0700, Jack <br*********@hotmail.comwrote:
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)
So, you don't _really_ want the thread aborted per se. You want the sound
stopped.

FWIW, it's reasonably important to be clear about what your _real_ goal
is. It's better to not presume some specific implementation, but rather
to focus on what the end effect you want is. Otherwise, you can get
caught up missing alternative approaches, because you spend all your time
trying to get some impossibility to work. :)
[...]
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.
Well, sure. That will allow you to know when the sound has finished
playing. But how does it address your (apparent?) question about stopping
a sound that's currently playing?

As far as that question goes, it's been awhile since I've played with
SoundPlayer. But it's possible that you can call the Stop() method on
your SoundPlayer instance, and the playback will be stopped and a
different thread waiting on the PlaySync() method will then be able to
continue (i.e. PlaySync() will return).

Pete
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Josh | last post: by
2 posts views Thread by snape | last post: by
1 post views Thread by Marco Krechting | last post: by
1 post views Thread by Lam | last post: by
5 posts views Thread by djc | last post: by
1 post views Thread by Sin Jeong-hun | last post: by
4 posts views Thread by gazza67 | last post: by
5 posts views Thread by gazza67 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.