How do you play sounds in a .NET app?

  • Thread starter Thread starter Will Pittenger
  • Start date Start date
so - an easy approach would be to make a ref. to the Visual Basic namespace
and use this function from within C#!

/Ole
 
I programmed only C++ until recently. My knowledge of VB is that it causes
more problems than it is worth. And, no I do not have VB. (Other than what
comes with Office.) Could you explain what you are talking about?
 
At this point, I am going with PlaySound. Have the paths for the sounds
that came with Windows changed over the years? I do not want to specifiy
the path unless it is constant. Referring to the sound by use, ie: "System
Notification", would be acceptable. But assuming path and filename does not
work.

Also, the only PlaySound that I found in MSDN is BOOL PlaySound(LPCSTR
pszSound, HMODULE hmod, DWORD fdwSound);. Did you enter your prototype by
hand?

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Ignacio Machin ( .NET/ C# MVP ) said:
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);




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 doubt of where your "heart" is located?

Anyways - here you have an example done from C#:

using System;

using System.Diagnostics;

using Microsoft.VisualBasic;

namespace TestConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

Microsoft.VisualBasic.Interaction.Beep();

}
}
}



/Claus Konrad
 
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.

As far as I know, if you want to stay inside .NET, you will have to
use DirectSound from the managed DirectX namespace - I just found this
out last week :)

You will need to download the huge DirectX SDK at
http://www.microsoft.com/downloads/...20-9DFD-4E7A-B947-3A037CCF84AF&displaylang=en

To use:


Namespaces:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Buffer = Microsoft.DirectX.DirectSound.SecondaryBuffer;

Define variables in your class:

/// <summary>
/// DirectSound: Secondary buffer to hold sound data.
</summary>
private SecondaryBuffer m_dsbSecondaryBuffer = null ;
/// <summary>
/// DirectSound: The Device object is used to create buffer objects,
/// manage devices, and set up the environment.
</summary>
private Device m_dsDevice = null ;

In the constructor:

// Initialize DirectSound device (sound card)
m_dsDevice = new Device( );
m_dsDevice.SetCooperativeLevel( this, CooperativeLevel.Priority );

In your play method:

// Create a DirectSound secondary buffer and fill it with audio data
// from the specified file. The buffer is automatically given the
// correct format and size (?).

try{ m_dsbSecondaryBuffer = new SecondaryBuffer(
strFileName, m_dsDevice ); }
catch( SoundException ){ return ; }

// Play the buffer.
if( null != m_dsbSecondaryBuffer )
m_dsbSecondaryBuffer.Play( 0, BufferPlayFlags.Default );


Hope this helps!
Jan Roelof
 
There is no way to directly play sounds in .NET. But you can use managed
DirectSound or a third party library.
 
Back
Top