views:

22

answers:

1

Hi,

I'm trying to play L1.wav which is in my resources folder. Previously I have pulled images from the resources file using the line btc.Properties.Resources.noImg which worked perfectly but if I try and do the same for the wav file I get a '...does not contain a definition for L1. Its there, works fine if I double click it. How do I get it to work?

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = btc.Properties.Resources.L1;
player.play();

Thanks.

A: 

The SoundLocation property requires a string that contains a file path or a URL. The resource you added is however returned as a Stream if it was a .wav file. You should have gotten a compile error message, saying that it can't convert an UnmanagedMemoryStream to a string.

This code worked well on my machine:

        System.Media.SoundPlayer player = new System.Media.SoundPlayer();
        player.Stream = Properties.Resources.test;
        player.Play();

What btc means in your source code is quite mysterious and possibly the real source of the compiler error message you quoted.

Hans Passant
btc is the project name. I tried this and am still getting the same error which is that the resources file does not contain a definition for L1 which is the wav file. Very odd.
flavour404
Why are you prefixing the project name? Where does the resource actually live? Have you added it to the correct project?
Hans Passant
the project name is not necessary and I get the same error if I do not include it and do as you did and just type Properties.Resources.L1. And yes I have added it to the correct project. As I have said, I have added jpg files to the resources folder and have no problem accessing them in exactly the same manner.
flavour404
I deleted the .wav files and then added one back. I did the same with a jpg file. The jpg file showed up fine but the .wav file did not, I am wondering if there is something to do with the .wav files themselves which is preventing the app from recognizing them.
flavour404
The compiler doesn't care about the .wav file when it looks for "L1". Post a screen shot of the Resources tab view.
Hans Passant
I added another resx file called sounds and now it works! My question is now what if I want to build a string to call the file because the one played is dependent on a few factors. For instance the current location (t_loci = 23) so the correct file would be 'L23' but now using a stream I am not sure how to get to that point?
flavour404
In all honesty I think I had set up the folders incorrectly.
flavour404