how to play embedded .wav sound resource in C# using SoundPlayer

Trying to create a simple app that plays a .wav file when you click a button...

target platform is windows mobile 6 CE 3.5 in C# using visual studio 2008 pro

in properties of solution, assembly name is listed as soundboard and default namespace is soundboard.

.wav file has been added as a resource, and set to embed in build options property for the resource

when compiling, and running on the windows mobile emuator, i get ZERO compilation errors, but when I click on the button all I get is a "system beep"

i am a total newb, can someone show me how to access the resource stream properly? i have a feel the path to the resource is not correct or im not looking it up properly....also I don't know how to use reflector

private void button1_Click(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            sp.Stream =
            this.GetType().Assembly.GetManifestResourceStream("soundboard.Properties.Resources.moo1");
            sp.Play();
        }

Open in new window

romerorAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Does it work this way?

            System.IO.MemoryStream ms = new System.IO.MemoryStream(Properties.Resources.moo1);
            SoundPlayer sp = new SoundPlayer();
            sp.Stream = ms;
            sp.Play();

(sorry...don't have WinCE)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In WinForms, you could just do:

        private void button1_Click(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            sp.Stream = Properties.Resources.moo1;
            sp.Play();
        }

Not sure if that will work in WinCE...
0
 
romerorAuthor Commented:
the error that returns is
Error      1      Cannot implicitly convert type 'byte[]' to 'System.IO.Stream'

on

            sp.Stream = Properties.Resources.moo1;
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
romerorAuthor Commented:
that compiled with zero issues, but crashed on the device emulator

error.jpg
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try passing the MemoryStream directly to the SoundPlayer constructor:

            SoundPlayer sp = new SoundPlayer(new System.IO.MemoryStream(Properties.Resources.moo1));
            sp.Play();

(sorry...can't test it out myself)
0
 
romerorAuthor Commented:
same crash on the device emulator....

i do not mind testing this out at all..I am trying to learn to program and I have been researching this particular problem for a couple of days and seem to be doing everything right just can't get it to work :)

this wil be a good documentation for SoundPlayer :)
0
 
romerorAuthor Commented:
I just checked to make sure it wasn't something quirky with the smartphone emulator i'm debugging/deploying on so I hooked up my phone, same exact results...

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could try using the PlaySound() API instead:
http://msdn.microsoft.com/en-us/library/ms229685(VS.80).aspx
0
 
romerorAuthor Commented:
wave file was corrupt or wrong type of file!

before using the file i checked to make sure device could play it as well as on pc

your solution worked as soon as I replaced the wav file with another one!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Haha...glad you figured it out.  =)
0
 
romerorAuthor Commented:
I *knew* it had to be something simple, even tho I'm a noob

I was only trying to play a simple wav file and it is currently 2010 i figured it couldn't be more than 2 or 3 lines of code lol

your first and second solutions were spot on......

thanks again for the persistence in trying to help! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.