This is a mirror of official site: http://jasper-net.blogspot.com/

Silverlight 5: Using the SoundEffect Class for Low-Latency Sound (and play WAV files in Silverlight)

| Thursday, April 14, 2011
In some applications, particularly touch-screen kiosk apps and casual games, it is desirable to be able to play a sound immediately upon a user action. For example, you may want to play a "click" sound when they press a key on an on-screen keyboard, or play a series of laser sounds when their on-screen spaceship fires its weapons.
Please note that this article and the attached sample code was written using the Silverlight 5 Beta, available at MIX11 in April 2011.
In previous versions of Silverlight, developers used a variety of tricks, including pre-loading sounds into a round-robin buffer of MediaElements in order to provide as low latency as possible. Windows Phone 7 introduced the XNA SoundEffect class to Silverlight developers. Silverlight 5 now includes that class as well.
Project Setup

Create a regular Silverlight 5 application. Most of the Xna libraries are in the core Silverlight runtime (some additional ones will be in the samples for the beta, and eventually in the SDK for release), so you don't need to add any additional references. Add some wav files to the project (see notes below about format). I have a couple included in the source code with this post. In MainPage.xaml, add a button and a click event handler. I called the button "ClickMe"
Loading and Playing a Sound Effect

Once you have a wav file in your project (as content), you can load it using the GetResourceStream method of the Application object. You then pass the stream to the FromStream method of the SoundEffect class and you're all set to play. Assuming you have a wav file in the root of your project, with its build action set to content and its name set to "laser_shot.wav", this is all you need to play sound. Put this code in the click event handler for the button.

var laserStream =
    Application.GetResourceStream(
        new Uri("laser_shot.wav", UriKind.RelativeOrAbsolute));
 
effect = SoundEffect.FromStream(laserStream.Stream);
effect.Play();

While that was really easy to do, with SoundEffect, the devil's in the details. SoundEffect is notoriously picky about what kind of wav files you provide it. Make sure you are using PCM encoded files, 8 or 16 bit mono or stereo (no 24 bit floating point samples) and either 22.5, 44.1 or 48khz sample rates. The SoundEffect class will fail to load other formats. This is consistent with the implementation on XNA desktop and XNA/Silverlight on the phone.
Controlling Volume, Pitch and Pan

The Play method shown above has an overload which accepts three float values for volume, pitch, and pan.

Read more: 10REM.net

Posted via email from Jasper-net

2 comments:

Anonymous said...

Now I am going to do my breakfast, after having my breakfast coming over again to read additional news.



Here is my site - Wie kann man sich die erfolgreiche und einfache glutenfreie Ernährung vorstellen

Anonymous said...

You really make it seem so easy together with your presentation however I in finding this topic
to be really one thing which I feel I'd never understand. It seems too complicated and extremely vast for me. I am taking a look ahead to your next publish, I will try to get the dangle of it!

Feel free to surf to my website ... Finanzberater in Wiesbaden