Link to home
Start Free TrialLog in
Avatar of romeror
romeror

asked on

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

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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...
Avatar of romeror
romeror

ASKER

the error that returns is
Error      1      Cannot implicitly convert type 'byte[]' to 'System.IO.Stream'

on

            sp.Stream = Properties.Resources.moo1;
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of romeror

ASKER

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

error.jpg
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)
Avatar of romeror

ASKER

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 :)
Avatar of romeror

ASKER

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...

You could try using the PlaySound() API instead:
http://msdn.microsoft.com/en-us/library/ms229685(VS.80).aspx
Avatar of romeror

ASKER

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!
Haha...glad you figured it out.  =)
Avatar of romeror

ASKER

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! :)