Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 00:45, 29 June 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to play audio in in XAML app using XNA library

From Wiki
Jump to: navigation, search

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.

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon XNA.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 7.1 SDK
Devices(s): Nokia Lumia 800
Platform(s): Windows Phone 7.5 (Mango)
Windows Phone 7.5
Created: sreerajvr (04 Apr 2012)
Last edited: hamishwillee (29 Jun 2013)

How To

  1. To start creating a new Windows Phone Application, start Microsoft Visual studio then create a new Project and select Windows Phone Application Template.
  2. In Visual Studio Solution Explorer, right-click References under your project and click Add Reference.
  3. In the Add Reference dialog box, click the .NET tab. Click 'Microsoft.Xna.Framework' entry to select it, and click OK.
  4. In the Solution Explorer panel, you will see the 'Microsoft.Xna.Framework' listed in the References folder of your project.
  5. 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)
  6. 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
  7. 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()
// Load the sound file
StreamResourceInfo info = Application.GetResourceStream(
new Uri("Audio/sound.wav", UriKind.Relative));
alarm = SoundEffect.FromStream(info.Stream);


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.

This page was last modified on 29 June 2013, at 00:45.
98 page views in the last 30 days.