Play multiple sound files in Silverlight for Windows Phone 7

by rahul 2/14/2011 11:10:20 PM

Windows Phone 7 doesn't support multiple MediaElement. In order to play multiple sounds in the same page, you may use the following approach.


    Add a reference to Microsoft.Xna.Framework

image

    Add the following using statements…

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio; 

    Add the sound files and ensure that Copy to Output directory is set to Content

image

    Add the following method to your page…

public void PlaySound(string soundFile)
{
    using (var stream = TitleContainer.OpenStream(soundFile))
    {
        var effect = SoundEffect.FromStream(stream);
        FrameworkDispatcher.Update();
        effect.Play();
        //Thread.Sleep(effect.Duration);
    }
}

    You can now call this Method with a valid Sound file (WAV file) and it should play.
    Notice the commented code above. If the sound file take a while to play, you may decide to uncomment this line so that the entire sound clip is played. This would be a blocking call though.

Until next time, Wave
Rahul

 


Quote of the day:
It is not necessary to understand things in order to argue about them. - Pierre Beaumarchais


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0