Using PlaySound from C#

M

mick

In my app I needed to play the odd sound. Initially I used the SoundPlayer
but found I had no control
over the volume so I decided to use unmanaged PlaySound. I play a sound from
a file but was wondering
how I would play one from a resource -

PlaySound(XXXX, IntPtr.Zero, SND_SYNC|SND_RESOURCE);

XXXX - What goes here?

mick
 
J

Jeroen Mostert

In my app I needed to play the odd sound. Initially I used the
SoundPlayer but found I had no control
over the volume so I decided to use unmanaged PlaySound.

PlaySound() gives you no control over the volume either and the current
implementation of SoundPlayer.Play() in fact delegates to PlaySound()
internally, so this would seem to be completely pointless. Using SoundPlayer
and ResourceManager makes it trivial to load the sound from a resource stream.

Controlling the volume *does* require P/Invoke to unmanaged functions like
waveOutSetVolume(). Code samples for doing this are plentiful on the Internet.
 
M

mick

Jeroen Mostert said:
PlaySound() gives you no control over the volume either and the current
implementation of SoundPlayer.Play() in fact delegates to PlaySound()
internally, so this would seem to be completely pointless. Using
SoundPlayer and ResourceManager makes it trivial to load the sound from a
resource stream.

Controlling the volume *does* require P/Invoke to unmanaged functions like
waveOutSetVolume(). Code samples for doing this are plentiful on the
Internet.

Yes, I should have mentioned I`m using waveOutSetVolume(). Because PlaySound
and waveOutSetVolume() are unmanaged I sort of connected the two together.
Anyway
Ive gone back to using SoundPlayer waveOutSetVolume but just out of interest
how would
I use PlaySound using a managed resource? (bit of code would make it
clearer).

TIA,

mick
 
J

Jeroen Mostert

[...] but just out of interest how would I use PlaySound using a managed
resource? (bit of code would make it clearer).
Well, to answer your question directly then: you wouldn't because you can't.
Managed resources are not stored as unmanaged resources, and unmanaged
functions can't access them by resource identifiers. You cannot include
unmanaged resources in your assembly except by editing the generated
executable (this applies to the C# compiler; C++/CLI does have support for
unmanaged resources, of course).

The reverse isn't possible either, by the way: you can't access unmanaged
resources with the managed classes (whether stored in a regular executable
or an assembly), with the exception of the specialized
Icon.ExtractAssociatedIcon(). This requires P/Invoking.
 
M

mick

Jeroen Mostert said:
[...] but just out of interest how would I use PlaySound using a managed
resource? (bit of code would make it clearer).
Well, to answer your question directly then: you wouldn't because you
can't. Managed resources are not stored as unmanaged resources, and
unmanaged functions can't access them by resource identifiers. You cannot
include unmanaged resources in your assembly except by editing the
generated executable (this applies to the C# compiler; C++/CLI does have
support for unmanaged resources, of course).

The reverse isn't possible either, by the way: you can't access unmanaged
resources with the managed classes (whether stored in a regular executable
or an assembly), with the exception of the specialized
Icon.ExtractAssociatedIcon(). This requires P/Invoking.

Thanks for your clear reply. I wish I`d asked here first instead of wasting
a few
hours searching for a non-existant solution on the web:)

Thanks again,

mick
 
J

Jeroen Mostert

[...] but just out of interest how would I use PlaySound using a managed
resource? (bit of code would make it clearer).
Well, to answer your question directly then: you wouldn't because you
can't. Managed resources are not stored as unmanaged resources, and
unmanaged functions can't access them by resource identifiers.

OK, so this isn't quite complete. It's true that unmanaged functions can't
access managed resources *directly*, but it's also true that PlaySound() can
play sounds directly from memory with the SND_MEMORY flag, and this is what
SoundPlayer uses: a managed resource can be read from memory just as well as
an unmanaged resource can (without copying, in fact). So if you really want
to do it yourself for some reason:

[DllImport("winmm.dll")]
public static extern unsafe bool PlaySound(byte* pszSound, IntPtr hmod,
int fdwSount);

public const int SND_NODEFAULT = 2;
public const int SND_MEMORY = 4;

...

unsafe {
PlaySound([resource].PositionPointer, IntPtr.Zero, SND_MEMORY |
SND_NODEFAULT);
}
 

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