How do you play sounds in a .NET app?

  • Thread starter Thread starter Will Pittenger
  • Start date Start date
W

Will Pittenger

I have a C# application that I would like to notify the user of events with
sound. Is there a way to playback at least the standard sounds?
 
have a look at the speech api for winforms. webforms deals with it using
scripting
 
take a look at

BOOL WINAPI PlaySound(
LPCSTR pszSound,
HMODULE hmod,
DWORD fdwSound
);

call this function at the event handler function...
 
Check out the playsound API and/or using process start passing in the sound
file name.
 
Hi Will,

Here is the code I"m using.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



//yyou need this namespace
using System.Runtime.InteropServices;
//P/invoke declarations

[DllImport("winmm.dll")]
public static extern int sndPlaySound(string lpszSoundName , int uFlags)
;

[DllImport("user32.dll")]
private static extern bool MessageBeep(int type);

//You use them like this:
MessageBeep( -1);

sndPlaySound( "file_to_play.wav" , 0);
 
There are no .NET equivalents?

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Ignacio Machin ( .NET/ C# MVP ) said:
Hi Will,

Here is the code I"m using.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



//yyou need this namespace
using System.Runtime.InteropServices;
//P/invoke declarations

[DllImport("winmm.dll")]
public static extern int sndPlaySound(string lpszSoundName , int uFlags)
;

[DllImport("user32.dll")]
private static extern bool MessageBeep(int type);

//You use them like this:
MessageBeep( -1);

sndPlaySound( "file_to_play.wav" , 0);




Will Pittenger said:
I have a C# application that I would like to notify the user of events with
sound. Is there a way to playback at least the standard sounds?
 
My application is not a web app. Your suggestion is of no help. The other
response where closer although they required importing functions. (Why
couldn't Microsoft predefine those imports?)

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Alvin Bruney said:
have a look at the speech api for winforms. webforms deals with it using
scripting

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Will Pittenger said:
I have a C# application that I would like to notify the user of events with
sound. Is there a way to playback at least the standard sounds?
 
no, but you can call it.
DllImport will solve your problem.

maybe this will help:

[DllImport("Coredll.dll")]

public static extern bool PlaySound(IntPtr rsc, IntPtr hMod, UInt32
dwFlags);

[DllImport("Coredll.dll")]

public static extern bool PlaySound(string Sound, IntPtr hMod, UInt32
dwFlags);

[DllImport("Coredll.dll")]

public static extern bool PlaySound(byte[] data, IntPtr hMod, UInt32
dwFlags);


also take a look at
http://www.dotnet247.com/247reference/msgs/26/130622.aspx
 
No,

You have to P/Invoke

The code I sent you works perfect

Will Pittenger said:
There are no .NET equivalents?

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:[email protected]...
Hi Will,

Here is the code I"m using.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



//yyou need this namespace
using System.Runtime.InteropServices;
//P/invoke declarations

[DllImport("winmm.dll")]
public static extern int sndPlaySound(string lpszSoundName , int uFlags)
;

[DllImport("user32.dll")]
private static extern bool MessageBeep(int type);

//You use them like this:
MessageBeep( -1);

sndPlaySound( "file_to_play.wav" , 0);




Will Pittenger said:
I have a C# application that I would like to notify the user of events with
sound. Is there a way to playback at least the standard sounds?
 
I would prefer something inside of .NET and have troubles believing that
Microsoft would not include sound in the .NET API. In general, I dislike
constantly importing Win32 stuff and then having to copy it everywhere I
need it. Either Microsoft should include all of Win32 in .NET (especially
if they keep calling .NET "better") or they should "preimport" all parts of
Win32 somewhere.
 
I know how to use DllImport. However, I dislike constantly importing stuff.
You always have to copy the import around. Microsoft should have a DLL
offering everything preimported (in a Win32 namespace) or just make it part
of .NET. (.NET is supposed to be better, right?)

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Vladimir Scherbina said:
no, but you can call it.
DllImport will solve your problem.

maybe this will help:

[DllImport("Coredll.dll")]

public static extern bool PlaySound(IntPtr rsc, IntPtr hMod, UInt32
dwFlags);

[DllImport("Coredll.dll")]

public static extern bool PlaySound(string Sound, IntPtr hMod, UInt32
dwFlags);

[DllImport("Coredll.dll")]

public static extern bool PlaySound(byte[] data, IntPtr hMod, UInt32
dwFlags);


also take a look at
http://www.dotnet247.com/247reference/msgs/26/130622.aspx

---
Vladimir Scherbina,
UA, Kiev.

Will Pittenger said:
Are there any .NET equivalents?
 
Is there a way to do this without Platform Invoke? I am getting tired of
constantly importing stuff. Especially since I have to copy it to each new
project.
 
Will Pittenger said:
I would prefer something inside of .NET and have troubles believing that
Microsoft would not include sound in the .NET API. In general, I dislike
constantly importing Win32 stuff and then having to copy it everywhere I
need it. Either Microsoft should include all of Win32 in .NET (especially
if they keep calling .NET "better") or they should "preimport" all parts
of
Win32 somewhere.


Take a look at the DirectX9 sdk (MSDN dowload area), the
Microsoft.DirectX.DirectSound namespace contains everything to play sounds
in managed winforms applications.

Willy.
 
Especially since I have to copy it to each new
to avoid this situation you can create your own wrapper and use it in all
projects.
 
there is no need for Microsoft (and time) to make wrappers for all functions
like this.
can you imagine the amount of code in this case ?

i suppose the simplest and most flexible way is to provide the P/Invoke
feature...
 
How easy is DirectX to use in a project that needs it only for one sound?
If this is swatting the fly with a nuclear bomb, I'll stick with the
platform invoke.
 
Will Pittenger said:
How easy is DirectX to use in a project that needs it only for one sound?
If this is swatting the fly with a nuclear bomb, I'll stick with the
platform invoke.


It's less involving than DirectX but more than PInvoke.

Here's basically what you need to do....


using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
// This to avoid name collision with System.Buffer
using Buffer = Microsoft.DirectX.DirectSound.Buffer;
....
private Device device;
private Buffer buffer;
.....

InitializeComponent();
// Set up DirectSound
device = new Device();
buffer = new Buffer(
"sound.wav", // Filename to load & Device to use
device
);
device.SetCooperativeLevel(this , // The window for the application
CooperativeLevel.Priority // The cooperative level
);
....
// Play sound
buffer.Play(
0, // Sound priority
BufferPlayFlags.Default // Play flags
);

Willy.
 
Back
Top