473,386 Members | 1,720 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,386 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
6 15913
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
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
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
Thanks very much for your help, Peter. I really appreciate it.

--
David Veeneman
Foresight Systems
Feb 24 '06
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
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

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

Similar topics

0
by: Bill | last post by:
Hi Everyone, I am having some difficulty in using the Windows Resource Localization Editor (WinRes.exe) on several Visual Basic .NET forms (.resx files) in two different projects. The editor...
3
by: J.Marsch | last post by:
Hello all: I am trying to build a resource dll that will house icons, and I'm afraid that I'm not having much luck reading it. Can you lend hand (or just point me to a good primer). Here's...
1
by: guy | last post by:
how do i refrence an image name "BackDrop.jpg" which i've added to the project as an embedded resource in runtime?
0
by: thompson_38 | last post by:
Is the difference between calling a HTML page by it location (axWebBrowser.Navigate("C:projecthome\home.htm" ref 0, ref 0, ref 0, ref 0)) and being able to access the frames in that page using ...
3
by: erajaysharma | last post by:
This issue is in Vista. I used XML files(one for DLLs, other for EXEs) ane embeded them in my EXEs and all DLLs ....actually i want to spawn a third party setup from found new hardware...but an...
2
by: Nesster13 | last post by:
Hi, I am building an application that will utilize the local resource file. Right now the application is separated into two part, the header and the content. One of the line at the top of the...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
0
by: Galen Somerville | last post by:
I'm not using Cultures so I just want to pull strings out of a Resource dll. In References there is a reference to the CDS80Eng.dll Partial code in a module --------------- Public Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.