M
mick
Is there any way to detect when a soundplayer (playing asynchronously)
has finished playing a sound?
mick
has finished playing a sound?
mick
Peter Duniho said:I assume you're talking about System.Media.SoundPlayer? AFAIK, no. But,
you can play the sound synchronously in a thread you have control over and
detect the end of the sound that way. For example:
SoundPlayer player = …;
Action<SoundPlayer> play = p =>
{
p.PlaySync();
MethodToCallWhenSoundIsDone();
};
play.BeginInvoke(player);
The above creates a delegate instance from an anonymous method that calls
PlaySync() and then calls a method when it's done. Then the
Action<SoundPlayer>.BeginInvoke() method causes the delegate instance to
be invoked on a thread pool thread asynchronously.
Of course, you can implement the threading aspect in any way you like; you
could even create a new class that inherits or composites the SoundPlayer
class and which adds a new event (e.g. "PlayCompleted") that is raised
when the synchronous-play-wrapped-in-a-thread finishes.
Pete
mick said:The problem of course if you start and stop the the player on the main
thread is you cant detect when user doesnt actually stop the sound but
lets it play to the end.