views:

657

answers:

1

I added a wav file to my Windows Mobile app and I want to use MobilePlaySound in CoreDll.dll to play it.

The fileName is one of its parameters:


MobilePlaySound(fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));

I create a new folder 'sound', add 'start.wav' to the project and set its property 'Build Action' to 'Embedded Resources'.

Then I set the fileName:


fileName = "\\Program Files\\myApp\\sound\\start.wav";

But the sound doesn't play at all. What's the correct filePath then?

+1  A: 

By setting the build action to "Embedded Resource" the file will be compiled into your assembly as a resource. This means that the wav file will be embedded into your .exe or .dll file, and it will not appear in the file system. Because of this, you can't pass a filename for the wav to some method that needs it.

There are two ways to solve it: if you really want to have the wav file as an embedded resouce, you will need to extract the resource and write it to a file in the file system at runtime. You can then pass the name of that file to the MobilePlaySound method. I would personally not choose this solution in this case.

The other solution is to not embed the wav file as a resource, but let it live as its own file in the file system. To achieve this, set the build action to "Content" and set the "Copy to Output Directory" setting to either "Copy always" or "Copy if newer". This will make the compiler include the file in the output. In this case, your assumption about where the file should be is correct.

In short:

  • Build action = "Content"
  • Copy to Output Directory = "Copy always" or "Copy if newer"
Fredrik Mörk
I've set the build action to "Embedded Resource", is the fileName correct? Why it refuse to play sound?
iPhoney
See my updated answer; I have explained a bit more in detail.
Fredrik Mörk
I've changed the build action to None and set Copy Always, but the sound still refuses to play. What might be the problem?
iPhoney
Hmm.. I don't know what I was smoking there... The build action should be "Content" not "None". Created a sample application and verified that it worked. Updated the answer text.
Fredrik Mörk
Thanks, I tested it on device and it works.
iPhoney