469,315 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Using PlaySound to play a resource with SND_RESOURCE

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().GetManifestResourc eStream("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

Aug 9 '06 #1
3 10276
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().GetManifestResourc eStream("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]
- mv*@spam.guard.caspershouse.com
"Jared" <go****@tripletreesoftware.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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().GetManifestResourc eStream("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

Aug 9 '06 #2
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


Nicholas Paldino [.NET/C# MVP] wrote:
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().GetManifestResourc eStream("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]
- mv*@spam.guard.caspershouse.com
"Jared" <go****@tripletreesoftware.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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().GetManifestResourc eStream("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
Aug 9 '06 #3
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().GetManifestResourc eStream("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);
}

Nicholas Paldino [.NET/C# MVP] wrote:
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().GetManifestResourc eStream("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]
- mv*@spam.guard.caspershouse.com
"Jared" <go****@tripletreesoftware.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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().GetManifestResourc eStream("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
Aug 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by CM | last post: by
3 posts views Thread by Harry J. Smith | last post: by
4 posts views Thread by rcattral | last post: by
2 posts views Thread by Joe Thompson | last post: by
10 posts views Thread by Ot | last post: by
9 posts views Thread by Andy | last post: by
6 posts views Thread by David Veeneman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.