Take the 2-minute tour ×

I'm trying to do something very basic, but for some reason it's just not clicking.

This is the code I have to get a sound from the resources and save them as a stream so I can use the sounds for nefarious purposes.

 /// Loads a wav file into an XNA Framework SoundEffect.
    private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        // For error checking, assume we'll fail to load the file.
        Sound = null;

            // Holds informations about a file stream.

            //StreamResourceInfo SoundFileInfo =  App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

            // Create the SoundEffect from the Stream
            Sound = SoundEffect.FromStream(SoundFileInfo.stream);

            soundEffectInstance = Sound.CreateInstance();
            soundEffectInstance.IsLooped = true;

        catch (NullReferenceException)
            // Display an error message
            MessageBox.Show("Couldn't load sound " + SoundFilePath);

Trying to change this code so that the file comes from an aboslute path on my server instead of the internal resources, but nothing seems to work. What am I doing wrong? Thanks.

share|improve this question
I'll post the answer I ended up using later today. –  Bob Jan 19 '12 at 6:51
add comment

1 Answer

You could use DynamicSoundEffectInstance to play sound from byte array. So, save downloaded sound into a binary file, and use it when needed.

Maybe, it helps you

share|improve this answer
@Ku6popr I keep getting an error that the byteArray is the wrong size, any ideas about this? –  Bob Jan 18 '12 at 17:29
DynamicSoundEffectInstance is used raw PCM wave data without any headers. The buffer must conform to the format alignment. BlockAlignment = BytesPerSample * AudioChannels, BytesPerSample = 2 bytes –  Ku6opr Jan 18 '12 at 18:58
Thanks. However, this turned out to not be the correct answer. Apparently when downloading the file, all the bytes are put into the array, so I couldn't use a buffer to add them to the file. I had to create a normal soundEffect with the fully bytearray, but it certainly put me on the right track. –  Bob Jan 19 '12 at 6:50
add comment

Your Answer


