473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 10598
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MLH | last post by:
I have a line of code that works when called from a procedure in Access 2.0 form... PlaySound("C:\cr\help\Help0018.wav", 0) I imported what I thought was needed into A97. However, running it...
0
by: CM | last post by:
There is a function called Playsound. But I think it can only play wav. I don't know how to use it in .net application to play MP3 or rm music. I am failed to find any sample code. Anyone can...
3
by: Harry J. Smith | last post by:
I added some sounds to my application, but the example in the msdn Library did not work. It had: public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags ); The...
4
by: rcattral | last post by:
Has anybody been using the PlaySound() function to play WAV files from within a DLL? Here is what I normally do. Since upgrading to 7.1 NET after using VC 6.0 for quite some time, I found that I...
2
by: Joe Thompson | last post by:
Hi, I am trying to use PlaySound in a VC++.net Windows app (VS 2003). I can use it to play a file but now I want to play it from a resource. I have two questions: How do I add a wav file to...
10
by: Ot | last post by:
I found information on PlaySound. I implemented it in my program and it works just dandy. The only little problem is that I have to package the ..wav files and send them along since PlaySound...
9
by: Andy | last post by:
Hi, I have an application that has several forms. Each form acts as a monitor of a gateway system. If the gateway appears to be dead, the application should play a .wav file (not resource)...
6
by: David Veeneman | last post by:
How do I configure a .NET 2.0 SoundPlayer object to play a WAV file embedded as a resource? I have tried the example in the MSDN documentation, and I am getting nowhere. Is there a simple...
0
by: poppy | last post by:
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.