Using PlaySound to play a resource with SND_RESOURCE

J

Jared

I'm using the first code sample below to play WAV files stored as
embedded resources. For some reason I *occasionally* get scratching
and crackling. I'm using a couple WAVs that ship with Windows, and
they play fine outside of my app.

This code works, but has the random "scratching" problem:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);
if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource
PlaySound(bytes, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
}

I'd like to try playing the resource WAVs directly, but I get no sound
with the following revised code:

private static void PlayResource(string audioRes)
{
// Note: 'MyObject' is a class in the same EXE as the embedded WAV
IntPtr HINSTANCE = Marshal.GetHINSTANCE(MyObject.GetType().Module);
PlaySound("MyApp.Media." + audioRes, HINSTANCE, SND_RESOURCE);
}

I suspect it has something to do with the way I'm specifying the
resource path, but the documentation on this is suspiciously absent.
I'm passing the same param value to both versions of my PlayResource()
method, and the param is the filename with extension (i.e.
"MySound.wav").

Can anybody see anything wrong with my second code sample?

TIA
Jared
 
N

Nicholas Paldino [.NET/C# MVP]

Jared,

My guess is that the problem is due to you calling PlaySound with the
SND_ASYNC. This will cause PlaySound to play the sound on another thread,
instead of the executing one. The function then immediately returns, and
then your function exists, making the byte array eligible for GC.

If you want to play the sound asynchronously, I would recommend creating
a thread pool task and executing that (or another thread if your thread pool
is pretty full already) like so:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);

if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource. Execute the code on a thread pool thread.
ThreadPool.QueueUserWorkItem(
delegate(object state)
{
// Play the sound.
PlaySound(bytes, IntPtr.Zero, SND_MEMORY);

// Get out.
return;
}, null);
}

This should work, and keep the bytes referenced alive. If not, then you
can just pass bytes as the state parameter to QueueUserWorkItem and then
cast it back to a byte array in the anonymous delegate.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Jared said:
I'm using the first code sample below to play WAV files stored as
embedded resources. For some reason I *occasionally* get scratching
and crackling. I'm using a couple WAVs that ship with Windows, and
they play fine outside of my app.

This code works, but has the random "scratching" problem:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);
if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource
PlaySound(bytes, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
}

I'd like to try playing the resource WAVs directly, but I get no sound
with the following revised code:

private static void PlayResource(string audioRes)
{
// Note: 'MyObject' is a class in the same EXE as the embedded WAV
IntPtr HINSTANCE = Marshal.GetHINSTANCE(MyObject.GetType().Module);
PlaySound("MyApp.Media." + audioRes, HINSTANCE, SND_RESOURCE);
}

I suspect it has something to do with the way I'm specifying the
resource path, but the documentation on this is suspiciously absent.
I'm passing the same param value to both versions of my PlayResource()
method, and the param is the filename with extension (i.e.
"MySound.wav").

Can anybody see anything wrong with my second code sample?

TIA
Jared
 
J

Jared

Thanks for your quick reply Nicholas. However, the code that works is
playing the WAV asynchronously. The code that doesn't work is NOT
playing it asynchronously, so I don't think GC is an issue. Btw, I've
tried the code below using both sync and async, to no avail.

In other words, the following code produces no sound, even though
HINSTANCE appears to be a valid hmod and my resource path points to the
embedded WAV:

IntPtr HINSTANCE = Marshal.GetHINSTANCE(MyObject.GetType().Module);
PlaySound("MyApp.Media.MySound.wav", HINSTANCE, SND_SYNC |
SND_RESOURCE);

TIA
Jared



Jared,

My guess is that the problem is due to you calling PlaySound with the
SND_ASYNC. This will cause PlaySound to play the sound on another thread,
instead of the executing one. The function then immediately returns, and
then your function exists, making the byte array eligible for GC.

If you want to play the sound asynchronously, I would recommend creating
a thread pool task and executing that (or another thread if your thread pool
is pretty full already) like so:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);

if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource. Execute the code on a thread pool thread.
ThreadPool.QueueUserWorkItem(
delegate(object state)
{
// Play the sound.
PlaySound(bytes, IntPtr.Zero, SND_MEMORY);

// Get out.
return;
}, null);
}

This should work, and keep the bytes referenced alive. If not, then you
can just pass bytes as the state parameter to QueueUserWorkItem and then
cast it back to a byte array in the anonymous delegate.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Jared said:
I'm using the first code sample below to play WAV files stored as
embedded resources. For some reason I *occasionally* get scratching
and crackling. I'm using a couple WAVs that ship with Windows, and
they play fine outside of my app.

This code works, but has the random "scratching" problem:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);
if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource
PlaySound(bytes, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
}

I'd like to try playing the resource WAVs directly, but I get no sound
with the following revised code:

private static void PlayResource(string audioRes)
{
// Note: 'MyObject' is a class in the same EXE as the embedded WAV
IntPtr HINSTANCE = Marshal.GetHINSTANCE(MyObject.GetType().Module);
PlaySound("MyApp.Media." + audioRes, HINSTANCE, SND_RESOURCE);
}

I suspect it has something to do with the way I'm specifying the
resource path, but the documentation on this is suspiciously absent.
I'm passing the same param value to both versions of my PlayResource()
method, and the param is the filename with extension (i.e.
"MySound.wav").

Can anybody see anything wrong with my second code sample?

TIA
Jared
 
J

Jared

For those who are interested, I solved my original problem
(popping/clicking) by using a thread pool task for async playback as
Nicholas suggested. I couldn't get his example to compile, so the code
I ended up with is shown below (Win32 imports omitted).

Note: I believe the popping/clicking was somehow caused by the fact
that the sound is playing in response to input on the serial port from
a barcode scanner. Playing the sound directly from, say, a button
click or on a thread pool task seems to have eliminated the
popping/clicking.

private static void PlaySound(object bytes)
{
PlaySound((byte[])bytes, IntPtr.Zero, SND_MEMORY);
}

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);
if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource on a new thread
ThreadPool.QueueUserWorkItem(new WaitCallback(PlaySound), bytes);
}


Jared,

My guess is that the problem is due to you calling PlaySound with the
SND_ASYNC. This will cause PlaySound to play the sound on another thread,
instead of the executing one. The function then immediately returns, and
then your function exists, making the byte array eligible for GC.

If you want to play the sound asynchronously, I would recommend creating
a thread pool task and executing that (or another thread if your thread pool
is pretty full already) like so:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);

if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource. Execute the code on a thread pool thread.
ThreadPool.QueueUserWorkItem(
delegate(object state)
{
// Play the sound.
PlaySound(bytes, IntPtr.Zero, SND_MEMORY);

// Get out.
return;
}, null);
}

This should work, and keep the bytes referenced alive. If not, then you
can just pass bytes as the state parameter to QueueUserWorkItem and then
cast it back to a byte array in the anonymous delegate.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Jared said:
I'm using the first code sample below to play WAV files stored as
embedded resources. For some reason I *occasionally* get scratching
and crackling. I'm using a couple WAVs that ship with Windows, and
they play fine outside of my app.

This code works, but has the random "scratching" problem:

private static void PlayResource(string audioRes)
{
// Get the resource into a stream
System.IO.Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Media."
+ audioRes);
if(null == str) return;

// Bring stream into a byte array
byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, (int)str.Length);

// Play the resource
PlaySound(bytes, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
}

I'd like to try playing the resource WAVs directly, but I get no sound
with the following revised code:

private static void PlayResource(string audioRes)
{
// Note: 'MyObject' is a class in the same EXE as the embedded WAV
IntPtr HINSTANCE = Marshal.GetHINSTANCE(MyObject.GetType().Module);
PlaySound("MyApp.Media." + audioRes, HINSTANCE, SND_RESOURCE);
}

I suspect it has something to do with the way I'm specifying the
resource path, but the documentation on this is suspiciously absent.
I'm passing the same param value to both versions of my PlayResource()
method, and the param is the filename with extension (i.e.
"MySound.wav").

Can anybody see anything wrong with my second code sample?

TIA
Jared
 

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