When does a sound file finish playing?

G

gazza67

Hi,

Does anyone know how to check for when a sound has finished playing?

I am currently using the SoundPlayer, there doesnt seem to be any
event for this - am I missing something?

Gary
 
M

Marc Gravell

Perhaps use PlaySync()? Then you know when it has finished... if you
need this async, then cheat (as below); note I haven't tested for null-
callback to use Play(), since I don't know without checking if the
Dispose() would stop the playing, and I like to clean up (Dispose())
after myself.

Marc

static void PlayAsync(string path, EventHandler callback) {
if (string.IsNullOrEmpty(path)) throw new
ArgumentNullException("path");
ThreadPool.QueueUserWorkItem(delegate {
using (SoundPlayer player = new SoundPlayer(path)) {
player.PlaySync();
}
if (callback != null) callback(null, EventArgs.Empty);
});
}
 
G

gazza67

Using PlaySync on a worker thread is a good idea and it works .....
however there is a problem.

I also need to be able to stop the sound from playing midway through
the sound if the user requests. I have tried runnning PlaySync in a
worker thread and the PlayStop on the UI thread but that doesnt seem
to work.

I am sort of surprised that this is so hard to do since stopping a
sound midway through and knowing when a sound has finished playing are
pretty basic things I would have thought.

I would also be interested in knowing how long a sound will take to
play before actually playing it, but I suppose that is asking a bit
much.

I can see I may have to start wading through the winmm .... yawn.....

Gazza
 
P

Peter Duniho

gazza67 said:
[...]
I am sort of surprised that this is so hard to do since stopping a
sound midway through and knowing when a sound has finished playing are
pretty basic things I would have thought.

They are pretty basic, I'd agree. But SoundPlayer is even more basic.
:) As near as I can tell, it really is only intended for extremely
simple "fire and forget" audio playback. Your initial stated goal is
achievable, but as you add more and more requirements, it's not
surprising to me that you're running into difficulty using the
SoundPlayer class.
I would also be interested in knowing how long a sound will take to
play before actually playing it, but I suppose that is asking a bit
much.

Well, from SoundPlayer yes. It's definitely not for that sort of thing.
I can see I may have to start wading through the winmm .... yawn.....

For what it's worth, you may find DirectX more suitable. DirectSound or
DirectShow would be appropriate, and there's even an existing
open-source .NET wrapper for DirectShow:
http://sourceforge.net/projects/directshownet/

I think either one (DShow or DSound) should provide enough functionality
to achieve the goals you've stated so far. Of course, if you have yet
more things you want to do, well... :)

Pete
 
G

gazza67

gazza67 said:
[...]
I am sort of surprised that this is so hard to do since stopping a
sound midway through and knowing when a sound has finished playing are
pretty basic things I would have thought.

They are pretty basic, I'd agree. But SoundPlayer is even more basic.
:) As near as I can tell, it really is only intended for extremely
simple "fire and forget" audio playback. Your initial stated goal is
achievable, but as you add more and more requirements, it's not
surprising to me that you're running into difficulty using the
SoundPlayer class.
I would also be interested in knowing how long a sound will take to
play before actually playing it, but I suppose that is asking a bit
much.

Well, from SoundPlayer yes. It's definitely not for that sort of thing.
I can see I may have to start wading through the winmm .... yawn.....

For what it's worth, you may find DirectX more suitable. DirectSound or
DirectShow would be appropriate, and there's even an existing
open-source .NET wrapper for DirectShow:http://sourceforge.net/projects/directshownet/

I think either one (DShow or DSound) should provide enough functionality
to achieve the goals you've stated so far. Of course, if you have yet
more things you want to do, well... :)

Pete

Peter,

Thanks for the link, looks like I need to invest some time into coming
to grips with DirectX.

No doubt you will see newbie style DX questions in the next week or so
pop up!!

Gazza
 
P

Peter Duniho

gazza67 said:
[...]
Thanks for the link, looks like I need to invest some time into coming
to grips with DirectX.

No doubt you will see newbie style DX questions in the next week or so
pop up!!

Well, for what it's worth, you will probably want to find a different
forum for those. DirectX and other unmanaged APIs are pretty off-topic
here; proportionally speaking you'll find a much larger audience able to
answer your questions in a newsgroup more appropriate to them, and of
course such questions should be avoided simply because they are off-topic.

Of course, in those newsgroups if you let on that you're using C#/.NET
they'll tell you that managed code questions are off-topic there. So
make sure you limit your questions in those forums to things that are
specifically about the DirectX API. :)

Hopefully, though, the .NET wrapper for DirectShow will provide a
sufficiently easy way to get at the API without having to know all the
gory details of DirectX. :)

Pete
 

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