How to play audio sounds in silverlight using XNA library
This article shows how to play audio sounds in silverlight using the XNA SoundEffect class. Silverlight has MediaElement element to play Audio and Video, but this element is very heavyweight for playing sound files. When it plays them, it stops all other media playback on the phone. So XNA library can be used for Small size/medium size sound files.
Article Metadata
Tested with
SDK: Windows Phone 7.1 SDK
Devices(s): Nokia Lumia 800
Compatibility
Platform(s): Windows Phone 7.5 (Mango)
Article
Created: sreerajvr
(04 Apr 2012)
Last edited: hamishwillee
(10 Apr 2013)
How To
- To start creating a new Windows Phone Application, start Microsoft Visual studio then create a new Project and select Windows Phone Application Template.
- In Visual Studio Solution Explorer, right-click References under your project and click Add Reference.
- In the Add Reference dialog box, click the .NET tab. Click 'Microsoft.Xna.Framework' entry to select it, and click OK.
- In the Solution Explorer panel, you will see the 'Microsoft.Xna.Framework' listed in the References folder of your project.
- Add a folder in your project and place your sound file in it (Here I created a folder 'Audio' and placed a sound file 'sound.wav' in it)
- In the source file that you want to use to program on XNA sound classes, add the following code in the using section of your code.
using Microsoft.Xna.Framework.Audio;// for SoundEffect
using System.Windows.Resources;// for StreamResourceInfo
- In your code, you will create an instance of the SoundEffect
Code snippet
Your entire code will look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Audio;// for SoundEffect
using System.Windows.Resources;// for StreamResourceInfo
namespace Audiotest
{
public partial class MainPage : PhoneApplicationPage
{
SoundEffect alarm;
// Constructor
public MainPage()
{
InitializeComponent();
// Load the sound file
StreamResourceInfo info = Application.GetResourceStream(
new Uri("Audio/sound.wav", UriKind.Relative));
alarm = SoundEffect.FromStream(info.Stream);
Microsoft.Xna.Framework.FrameworkDispatcher.Update();
alarm.Play();
}
}
}
Summary
Your audio files must be .wav files (The SoundEffect.FromStream() method only works with PCM wave audio). XNA sound library works if you frequently call the FrameworkDispatcher.Update() method.This is common in XNA game apps, because they are designed around in a game loop that runs the code every frame.


Hamishwillee - Thanks, other things you can do
Hi sreerajvr
Thanks for this article. I've subedited slightly and added a link to the SoundEffect class.
It would help if there were a little more description about what your code is doing - for example why is the FrameworkDisplatcher needed? Other questions I as a reader wanted to know:
Regards
Hamishhamishwillee 08:40, 1 May 2012 (EEST)