Over a million developers have joined DZone.

Playing sounds in Windows Phone 7 applications

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

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/

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}