473,785 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does a sound file finish playing?

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

Sep 17 '07 #1
5 11135
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(strin g path, EventHandler callback) {
if (string.IsNullO rEmpty(path)) throw new
ArgumentNullExc eption("path");
ThreadPool.Queu eUserWorkItem(d elegate {
using (SoundPlayer player = new SoundPlayer(pat h)) {
player.PlaySync ();
}
if (callback != null) callback(null, EventArgs.Empty );
});
}

Sep 18 '07 #2
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


On Sep 18, 4:22 pm, Marc Gravell <marc.grav...@g mail.comwrote:
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(strin g path, EventHandler callback) {
if (string.IsNullO rEmpty(path)) throw new
ArgumentNullExc eption("path");
ThreadPool.Queu eUserWorkItem(d elegate {
using (SoundPlayer player = new SoundPlayer(pat h)) {
player.PlaySync ();
}
if (callback != null) callback(null, EventArgs.Empty );
});
}

Sep 19 '07 #3
gazza67 wrote:
[...]
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
Sep 19 '07 #4
On Sep 19, 11:08 am, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.com>
wrote:
gazza67 wrote:
[...]
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

Sep 19 '07 #5
gazza67 wrote:
[...]
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
Sep 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3535
by: Paul Moore | last post by:
Does anyone know of a Python library which handles sound file formats (MP3, Ogg, FLAC are the three I'm nost interested in). I'd like something that can manipulate "metadata" - stuff like artist, title, album tags. I'm not critically interested in format conversion, or playing sound files - I can handle that with other applications. I'm using Windows, and Python 2.3, if it matters (as it might for C extensions). I can manage with just...
1
1736
by: Vanessa | last post by:
Anyone know how to embed sound file to vb.net project, then play the sound file? I have searched the web for example... and download the example and when run, it works perfectly. But when i try to insert a sound file (.wav) to my project and set the Build Action to "Embedded Resources". And then I use the same code as what I saw in the example, but it gives me an error,
1
8008
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 ----------------------------------------------------------------------------...
6
2432
by: martin1 | last post by:
Hi, All, The app retrieve sound.wav file from my loacal C drive, is there anyway the sound file can be stored in the app? then when building, it goes with app rather than sending sound file seperate with app when deploying the app. Thanks
2
5884
by: hayz | last post by:
Flash sound file looping problems hello there I'm definitely a newb so please bare some patience. I have a flash sound file on the index page of a site i'm working on. First off i need the .swf file to continuously loop, secondly i need this same file to play on every page visited on the site. I've researched and tried to just follow examples but each time there's no loop. The file plays once every time the page loads and then ends. ...
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
1
2488
by: Sheena777 | last post by:
I want to Play a specific sound file in my code when a certain event happens, but i don't know how. I can play the system sounds in my program and I know how to start the media player to play an mp3 but I want to be able to play the sound file without an external player. Is there any other way of playing a sound file in vb.net code when an event is fired?
3
7839
by: Jack | last post by:
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()
0
1367
by: nicolecastel | last post by:
Hello I'm developping an application in Csharp,I want to play a sound file in an external sound card,is there a function which give me the possibility to play sound using the name of the audio file and the name( or the ID) of the sound card? Thank you in advance :)
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
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
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.