473,770 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PlaySy nc() 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 7837
On Mon, 02 Jun 2008 03:52:51 -0700, Jack <br*********@ho tmail.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...@nn owslpianmk.com>
wrote:
On Mon, 02 Jun 2008 03:52:51 -0700, Jack <bradnerd...@ho tmail.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*********@ho tmail.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
4126
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 SourceDataLine. currently, it does not play back. andrewjj20 the play method: class Player implements Runnable { public Player() {
2
1825
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
3181
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 access but a really, like Virus Alert kind of warning? Regards Marco
1
8006
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" sound instead playing the file (same code works fine in a windows application written in c# , that mean there is nothing wrong with my sound card.) thanks ----------------------------------------------------------------------------...
5
3589
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 wav files for sound effects as well as playing longer sound clips in mp3 format. can anyone point me in the right direction?
1
3872
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 the sound? I looked for some event, but there's nothing but Disposed events.
4
5072
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 or music which is: OLE.DoVerb (vbOLEPrimary) But the one to stop or interupt the sound I could not find it. Please let know
4
1693
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
11133
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
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.