473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 10983
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);
});
}

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...@gmail.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(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);
});
}

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...@NnOwSlPiAnMk.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
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,...
1
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...
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"...
6
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...
2
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...
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
1
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...
3
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:...
0
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.