472,951 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 7591
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josh | last post by:
I am having a problem with sound in my program, it takes information from a TargetDataLine, put's it in a temporary file, then takes it back out of the temporary file and plays it using a...
2
by: snape | last post by:
Is it possible to have an event trigger when the sound file for BGSOUND is finished downloading? Or just to know when the file is loaded and playing? Thank you.
1
by: Marco Krechting | last post by:
Hi All, I have a database with the timer function set to warn the user that arecord is due to expire. Is there a Win API that I can use so a loud warning will go off. Not this simpel beep in...
1
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be"...
5
by: djc | last post by:
what choices do I have to play sounds in a program using vb.net? Or, I guess if nothing vb specific what windows API(s) should I look at? I am interested to find out whats needed for playing short...
1
by: Sin Jeong-hun | last post by:
Microsoft.DirectX.DirectSound.SecondaryBuffer buff=new ... .... buff.Play(0,data,BufferPlayFlags.Default); The play method seems to be asynchronous. Then how can I know when it finished playing...
4
by: Benzerari | last post by:
Hi All; Please, if any one knows the visual basic code, to be added to a Stop button in order to stop playing some sound of (*.Wav) type. I have achieved to find the code for start playing sound...
4
by: gazza67 | last post by:
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
5
by: gazza67 | last post by:
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
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.