Play Embedded Sound Resource?

J

Joe Cool

I found this link a few years ago:

http://msdn.microsoft.com/en-us/library/3w5b27z4.aspx

And when I first tried it, it worked fine. I now have a really simple
app for personal use in mind and I want to be able to play an embedded
sound resource, but this code no longer seems to work.

The Assembly.GetManifestResourceStream is returning a null value.

Any ideas?

TIA,
 
M

Mike Lovell

I found this link a few years ago:
http://msdn.microsoft.com/en-us/library/3w5b27z4.aspx

And when I first tried it, it worked fine. I now have a really simple
app for personal use in mind and I want to be able to play an embedded
sound resource, but this code no longer seems to work.

The Assembly.GetManifestResourceStream is returning a null value.

Any ideas?

Yep... You definitely embedded it? By default this is NOT the case, you
need to make sure in Properties on the sound file, "Build Action" is
"Embedded Resource", otherwise you will get a null.

Is it in a folder or just in the 'root' of your project?

Remember it needs the full name including namespace, so if my sound file was
called 'music.wav' in a folder called 'sounds' and my namespace was
'GoTinker.MyProject'

GoTinker.MyProject.Sounds.music.wav

If you really can't find it, do a:

GetManifestResourceNames()

A print each array item to screen, that will show you where it's hiding!
 
J

Joe Cool

Yep... You definitely embedded it?   By default this is NOT the case, you
need to make sure in Properties on the sound file, "Build Action" is
"Embedded Resource", otherwise you will get a null.

Is it in a folder or just in the 'root' of your project?

Thanks for your interest in my dilemma and your response. Here is how
I added the wav file as a resource.

I opened the project's properties window and selected the resources
tab. I clicked on the dropdown that had the value "Strings" and
selected Audio. I then clicked on Add Resource dropdown and selected
Add Existing File. I had made sure that the wav file I wanted to add
was in the root of the project's project folder. I selected that file
from the add file dialog. The wav file appeared as an icon in the
content part of the window. I can right click on the icon and click
play and the wav file plays fine. The access modifier dropdown is
Internal.

Does this help?
Remember it needs the full name including namespace, so if my sound file was
called 'music.wav' in a folder called 'sounds' and my namespace was
'GoTinker.MyProject'

According to the link I provided, the says that you must specify the
resource in the format:

<AssemblyName>.wavefilename.wav

The angle brackets are required and AssemblyName is case sensitive.
Likw I also said, this used to work. I also made sure the wav file
name was also specfied in the same case as the actual filename I added
which is the same as the resource name label under the icon. In my
case, the assembly name is PlayResourceSoundDemo. I have single
stepped through the code and examined the value of
assembly.GetName().Name and is definitely PlayResourceSoundDemo.
GoTinker.MyProject.Sounds.music.wav

If you really can't find it, do a:

GetManifestResourceNames()

A print each array item to screen, that will show you where it's hiding!

I examined the value returned by this method in the debugger and two
items are listed:

{string[2]}
[0]: "PlayResourceSoundDemo.Properties.Resources.resources"
[1]: "PlayResourceSoundDemo.Form1.resources"
 
M

Mike Lovell

I opened the project's properties window and selected the resources
tab. I clicked on the dropdown that had the value "Strings" and
selected Audio. I then clicked on Add Resource dropdown and selected
Add Existing File. I had made sure that the wav file I wanted to add
was in the root of the project's project folder. I selected that file
from the add file dialog. The wav file appeared as an icon in the
content part of the window. I can right click on the icon and click
play and the wav file plays fine. The access modifier dropdown is
Internal.

Okay, this time just right click on your project and add an existing file.
Select your wav files and add it to the project, then select the wav file
from the solution explorer. Set its "Build Action" to "Embedded Resource",
save the project.
I examined the value returned by this method in the debugger and two
items are listed:

{string[2]}
[0]: "PlayResourceSoundDemo.Properties.Resources.resources"
[1]: "PlayResourceSoundDemo.Form1.resources"

Run it again after doing the above and your wav file should turn up. Then
use the name in your "GetManifestResourceStream" call
 
J

Joe Cool

I opened the project's properties window and selected the resources
tab. I clicked on the dropdown that had the value "Strings" and
selected Audio. I then clicked on Add Resource dropdown and selected
Add Existing File. I had made sure that the wav file I wanted to add
was in the root of the project's project folder. I selected that file
from the add file dialog. The wav file appeared as an icon in the
content part of the window. I can right click on the icon and click
play and the wav file plays fine. The access modifier dropdown is
Internal.

Okay, this time just right click on your project and add an existing file..
Select your wav files and add it to the project, then select the wav file
from the solution explorer.  Set its "Build Action" to "Embedded Resource",
save the project.
I examined the value returned by this method in the debugger and two
items are listed:
{string[2]}
   [0]: "PlayResourceSoundDemo.Properties.Resources.resources"
   [1]: "PlayResourceSoundDemo.Form1.resources"

Run it again after doing the above and your wav file should turn up.  Then
use the name in your "GetManifestResourceStream" call

Thanks a lot for your help. I found that the angle brackets should not
delimit the assembly name in the GetManifestResourseStream method.
Otherwise, your suggestions worked.
 
J

Joe Cool

I opened the project's properties window and selected the resources
tab. I clicked on the dropdown that had the value "Strings" and
selected Audio. I then clicked on Add Resource dropdown and selected
Add Existing File. I had made sure that the wav file I wanted to add
was in the root of the project's project folder. I selected that file
from the add file dialog. The wav file appeared as an icon in the
content part of the window. I can right click on the icon and click
play and the wav file plays fine. The access modifier dropdown is
Internal.

Okay, this time just right click on your project and add an existing file..
Select your wav files and add it to the project, then select the wav file
from the solution explorer.  Set its "Build Action" to "Embedded Resource",
save the project.
I examined the value returned by this method in the debugger and two
items are listed:
{string[2]}
   [0]: "PlayResourceSoundDemo.Properties.Resources.resources"
   [1]: "PlayResourceSoundDemo.Form1.resources"

Run it again after doing the above and your wav file should turn up.  Then
use the name in your "GetManifestResourceStream" call

Out of curiosity, I wonder why the way I did it did not work. After
all, I did add the wav file as a resource.
 
M

Mike Lovell

Out of curiosity, I wonder why the way I did it did not work. After
all, I did add the wav file as a resource.

Well you know what they say, all's well that ends well!
 

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