Silverlight & XNA together on WP7
I am developing most of my WP7 apps in Silverlight 3, then doing whatever porting is necessary to get them running on WP7 (the final stage is then optimising for WP7, but that’s another story). ‘Nuke Your Office’ (http://apps.webservicesuk.com/NukeYourOffice) has a lot going on sound-wise (multiple WMA files fired at random and in parallel). All was fine in SL3 but when I moved to WP7 I realised that I had an issue with performance. I recalled reading something about being able to use XNA functionality for playing sounds within Silverlight/WP7 – A little searching later and I came across the “Physics helper Library” that already contained just the functionality I was looking for. Within the downloadable source code there is a file called SOUNDMAIN.CS. All that is required is to include this file in your Silverlight Desktop and/or WP7 Silverlight project and provide a reference to the following XNA libraries:
using Microsoft.Xna.Framework.Audio; using System.IO; using Microsoft.Xna.Framework;
Then, after setting up your sound files:
#if WINDOWS_PHONE Whistle = new SoundMain(this.LayoutRoot, "sounds/bomb.wav", 2, 0); Whistle2 = new SoundMain(this.LayoutRoot, "sounds/bomb2.wav", 2, 0); Explosion1 = new SoundMain(this.LayoutRoot, "sounds/Explosion1.wav", 2, 0); Explosion2 = new SoundMain(this.LayoutRoot, "sounds/Explosion2.wav", 2, 0); Explosion3 = new SoundMain(this.LayoutRoot, "sounds/Explosion3.wav", 2, 0); Siren = new SoundMain(this.LayoutRoot, "sounds/AirRaid.wav", 2, 0); CrackGlass = new SoundMain(this.LayoutRoot, "sounds/GlassCracking.wav", 2, 0); #else Whistle = new SoundMain(this.LayoutRoot, "sounds/bomb.wma", 2, 0); Whistle2 = new SoundMain(this.LayoutRoot, "sounds/bomb2.wma", 2, 0); Explosion1 = new SoundMain(this.LayoutRoot, "sounds/Explosion1.wma", 2, 0); Explosion2 = new SoundMain(this.LayoutRoot, "sounds/Explosion2.wma", 2, 0); Explosion3 = new SoundMain(this.LayoutRoot, "sounds/Explosion3.wma", 2, 0); Siren = new SoundMain(this.LayoutRoot, "sounds/AirRaid.wma", 2, 0); CrackGlass = new SoundMain(this.LayoutRoot, "sounds/GlassCracking.wma", 2, 0); #endif
You can play them within your code as follows:
Whistle.Play();
NB the only limitation is that the sound files for WP7 and XNA must be in WAV format (WMA is not supported under XNA on WP7).
My thanks to Andy Beaulieu and the other developers on the CodePlex site http://physicshelper.codeplex.com/
Please ensure that you are familiar with the licensing conditions if you plan on using this code in your own app.
Can I simply say what a relief to find an individual
who really knows what they are talking about on the web.
You actually know how to bring an issue to light and make
it important. More and more people have to look at this and understand this
side of the story. It’s surprising you aren’t more popular because you certainly have the gift.
http://www.homepage-technologies.co.uk said this on July 2, 2012 at 9:00 pm |