Play sounds in Metro-style apps #win8dev #winrt

Wordastic for Windows 8 is close to completion. As a part of enhancing the app, I updated word count from 5k to 25k. I also added some sounds. Bach in the background and some sounds depending upon correct user guess.

So I did it all and I came to the sounds… figured out how to play background music but was not how to play multiple sounds. Here’s what I did

I added an instance of MediaElement and set the source to sound 1.
Set another 2 instance and set the source to sound 2 and sound 3.

And all I do is enable it / play it in code behind.

<MediaElement Name="mediaControl" IsLooping="True" AutoPlay="False" Height="0" Width="0" Source="Assets/bach-bwv848a-grant.mp3" />
<MediaElement Name="meYahoo" IsLooping="False" AutoPlay="False" Height="0" Width="0" Source="Assets/Yahoo.wav" />
<MediaElement Name="meSlip" IsLooping="False" AutoPlay="False" Height="0" Width="0" Source="Assets/FunnySlip.wav" />
mediaControl.AutoPlay = WordasticConfig.PlaySounds;
if(WordasticConfig.PlaySounds)
{
    if (bValidWord)
        meYahoo.Play();
    else
        meSlip.Play(); 
}

These links should help
Quickstart: Creating a media player app (Metro style apps using C#/VB/C++ and XAML)

MediaElement Class

Note: SoundEffect and SoundEffectInstance in WPDev etc where exposed by XNA. Windows 8 doesn’t directly support XNA so this is the only mechanism in Win RT unless you want to play with Direct X. If you want do so do, please have a look at Sharp DX which provides DX wrapper for .NET

About these ads

2 thoughts on “Play sounds in Metro-style apps #win8dev #winrt

  1. Pingback: Windows 8 Developer Links – 2012-07-10Dan Rigby | Dan Rigby

  2. Pingback: Play Sounds in Metro-style Applications

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s