471,319 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

using SoundPlayer with resource?

How do I configure a .NET 2.0 SoundPlayer object to play a WAV file embedded
as a resource?

I have tried the example in the MSDN documentation, and I am getting
nowhere. Is there a simple example of how to do this, or can someone provide
an example? Thanks.

David Veeneman
Foresight Systems
Feb 23 '06 #1
6 15728
David,
I haven't spent much time looking at the SoundPlayer object, but if there is
no obvious way to play an embedded resource, you will probably need to
P/Invoke the PlaySound API in Winmm.dll.
Here's a short article that illustrates:

http://www.eggheadcafe.com/articles/20030302.asp

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"David Veeneman" wrote:
How do I configure a .NET 2.0 SoundPlayer object to play a WAV file embedded
as a resource?

I have tried the example in the MSDN documentation, and I am getting
nowhere. Is there a simple example of how to do this, or can someone provide
an example? Thanks.

David Veeneman
Foresight Systems

Feb 23 '06 #2
Thanks, Peter-- I used to P/Invoke before .NET 2.0. But MSDN says the
SoundPlayer class can play embedded WAVs:

http://msdn2.microsoft.com/en-us/library/3w5b27z4.aspx

It even provides an example, which I can't get to work. Could you take a
look at it and tell me if it makes sense to you? Thanks.

David Veeneman
Foresight Systems
Feb 23 '06 #3
I think the example talks about playing a sound from an embedded "resource",
not necessarily from a file that has been marked as such in it's build
properties.

Here is a link that may provide more insight:

http://msdn2.microsoft.com/en-us/lib.../t69a74ty.aspx

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"David Veeneman" wrote:
Thanks, Peter-- I used to P/Invoke before .NET 2.0. But MSDN says the
SoundPlayer class can play embedded WAVs:

http://msdn2.microsoft.com/en-us/library/3w5b27z4.aspx

It even provides an example, which I can't get to work. Could you take a
look at it and tell me if it makes sense to you? Thanks.

David Veeneman
Foresight Systems

Feb 24 '06 #4
Thanks very much for your help, Peter. I really appreciate it.

--
David Veeneman
Foresight Systems
Feb 24 '06 #5
I found my answer--it really is very simple! Thanks to Peter Bromberg for
steering me in the right direction.

To use the .NET 2.0 SoundPlayer class with a project resource, you simply
call the resource by name, without quotes. Here is a complete example:

1) Create a new .NET 2.0 console application.

2) Right-click the project's Properties icon and select 'Open' from the
contect menu that appears.

3) Select the Resources tab of the project's properties pages. If the
Resources page says you don't have a default resources file, click on the
link on the page to create one. Ignore the blank string resource it shows
you.

4) In the property page toolbar, select 'Add Resource' > 'Add existing
file...' Navigate to 'c:\windows\media\notify.wav' and open it. The first
button on the Resources page toolbar should now say 'Audio', and the
property page should show a Windows Media document icon named 'notify'.

5) Replace all code in Program.cs with the code shown below.

6) Run the application. The Windows 'notify' sound should play as soon as
the program launches. Hit any key to exit the program.

7) You can use any sound added as a project resource by simply specifying
it, using the 'Project.Resources' syntax shown in the code. In VS.NET,
Intellisense will show the list of available resources.

using System;
using System.Collections.Generic;
using System.Text;
using System.Media;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SoundPlayer sound = new
SoundPlayer(Properties.Resources.notify);
sound.Play();
Console.ReadKey();
}
}
}
Feb 24 '06 #6
I found my answer--it really is very simple. Add the sound to the projects
resources using the project's property pages. For example, let's say you
add the .WAV file 'Windows Pop up Blocked.wav' to your project's resources.
Then, add the following declaration to the code for the class in which you
want to use the sound:

SoundPlayer m_MySound = new
SoundPlayer(Properties.Resources.Windows_Pop_up_Bl ocked);

The 'Properties.Resources.Windows_Pop_up_Blocked' part of the statement is
simply a standard call to a project resource, in this case, the sound that
you added.

To play the sound, invoke it as follows:

m_MySound.Play();
--
David Veeneman
Foresight Systems
Mar 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by J.Marsch | last post: by
reply views Thread by thompson_38 | last post: by
2 posts views Thread by Nesster13 | last post: by
61 posts views Thread by Sanders Kaufman | last post: by
reply views Thread by Galen Somerville | last post: by

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.