Adding Sound Effects to a Windows Phone 7 Silverlight Application
Join the DZone community and get the full member experience.
Join For Freecertain windows phone 7 applications benefit from using sound effects, even if the application itself is written in silverlight. you most likely have noticed that you can play media inside a silverlight application by making use of the mediaplayerlauncher inside your application to pass control to the integrated media player on the phone or by making use of the mediaelement class. both these options are great if you want to play audio content under control of some form of media player (with mediaelement allowing to integrate player functionality inside your own application).
however, if you want short sound effects that play under control of
your application, you can make use of functionality that is available in
the xna framework, even if you are developing a silverlight based
windows phone 7 application. the xna framework contains a
soundeffect
class. this class holds a sound resource in memory that can be played
from inside your application by calling its play method. you can even
alter properties like the pitch and volume of a soundeffect. since there
is interoperability possible between silverlight and the xna framework
(at least to a certain extend), it is possible to make use of
soundeffect inside a silverlight application. however, given the
different application models for silverlight and xna framework applications
,
you have to be aware of what you are doing when added soundeffect to
your silverlight application. a first attempt to add sound might look
like this:
using system.componentmodel; using system.io; using microsoft.xna.framework; using microsoft.xna.framework.audio; namespace clicker.model { public class soundplayer { public enum sounds { switch = 0, penalty, startgame, highscore, endgame }; static soundeffect[] soundeffects; public static void play(sounds soundeffect) { if (soundeffects != null) { soundeffects[(int)soundeffect].play(); } } } }
this code snippet, taken from a real application, shows how to play a
sound that was previously loaded into memory. to focus on playing
sounds, loading the sounds into an array of type soundeffect is omitted.
when the static play method of the soundplayer class is called, an
exception is thrown.
the exception being displayed clearly indicates the cause of the
problem. you can read more about solving this particular exception on
msdn, which contains information about enabling
xna framework events inside windows phone applications
. the information is very relevant, however, it explains how you should regularly call
frameworkdispatcher.update()
.
calling this method dispatches messages that are in the xna framework
message queue for processing. the documentation gives the advice to call
frameworkdispatcher.update()
regularly, for instance
on a timer. for playing single sound effects, this seems to be an
overkill. after all, using a timer means that some code will executed
repeatedly (in our case even if no sound is currently played). on a
battery powered device like a windows phone 7 this means that we are
draining more battery power then necessary. instead, i simply modified
my code by adding a call to
frameworkdispatcher.update()
immediately before calling the
play
method on the
soundeffect
object. this works fine in the following scenario:
- the only xna framework functionality used inside a silverlight application is one or more instances of type soundeffect.
- only one single soundeffect is played at any given time.
the following code snippet shows the modified play method of my own soundplayer class (which is playing sounds as expected):
public static void play(sounds soundeffect) { if (soundeffects != null) { frameworkdispatcher.update(); soundeffects[(int)soundeffect].play(); } }
in an upcoming blog entry i will show you how to load a number of soundeffects into memory using a backgroundworker.
source:
http://mstruys.com/2011/01/24/adding-sound-effects-to-a-windows-phone-7-silverlight-application/
Opinions expressed by DZone contributors are their own.
Comments