Mobile Zone is brought to you in partnership with:

I have worked for several companies as a senior software developer and lead developer. I bave enterprise level programming experience on Web, Silverlight, and Windows Phone 7. I am a MVP in Device Application Development since 2011. Jevgeni is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Playing sounds in Windows Phone 7 applications

11.25.2011
| 5478 views |
  • submit to reddit

In this short tutorial I will show how to play sounds in Windows Phone 7 Silverlight applications. You will be surprised that for playing sounds in Silverlight application you will need to add a XNA Framework reference to your project.

eugenedotnet sounds in windows phone 7 applications

1. Adding reference

First of all you need to add XNA Framework reference to your project. To play sounds in Silverlight applications we are going to use Microsoft.XNA.Framework library.

adding xna framework reference

2. Adding namespaces

Next you need to add two XNA Framework references to your code. You need to add it to a class which is going to be used for playing sounds.

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

3. Creating a method

Finally, we need to created a method for playing sounds within a class. I have used the following code:


private void PlaySound(string path)
{
    if (!string.IsNullOrEmpty(path))
    {
        using (var stream = TitleContainer.OpenStream(path))
        {
            if (stream != null)
            {
                var effect = SoundEffect.FromStream(stream);
                FrameworkDispatcher.Update();
                effect.Play();
            }
        }
    }
}

4. Using method

I have used the following code to test my SoundController class:

SoundController sc = new SoundController();
sc.PlaySound(@"Sounds\show.wav");

Source: http://www.eugenedotnet.com/2010/10/w13-playing-sounds-in-windows-phone-7-applications/
Published at DZone with permission of Jevgeni Tšaikin, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Robert Craft replied on Thu, 2012/01/26 - 7:11am

Also, when you deploy the app to your actual phone device through VS 2010 and Zune make certain you unplug or close Zune first before attempting to play sound otherwise they will fail to load. This article helped after spending hell lot of time with mediaelement. When I tried using this tip, it worked…

Spring Security

Sirikant Noori replied on Fri, 2012/03/30 - 1:54pm

 

Thanks buddy. It was a great article to read and was the very first article that really explained that how shall sounds be played in a simple and awesome manner.

When I started using info present in this article, it worked. It help me greatly after spending a lot of time.

 Thnx

Java Exam

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.