473,395 Members | 1,608 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,395 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 11009
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...

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.