using SoundPlayer with resource?

D

David Veeneman

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
 
D

David Veeneman

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();
}
}
}
 
D

David Veeneman

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_Blocked);

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top