List of 1 thing in Win8, Windows 8: Playing media files

As I stated in my previous blog, App Bars and much of the documentation (read stuff I can reuse on my blog) hasn’t been written by the boffins in Redmond.  What is a boffin?  I have no idea and hope it isn’t an insult, but the British use the term to refer to smart, apparently a now defunct restaurant.

Oh well, the boffins in Redmond, are working hard to create good documentation and I am working hard to find all of the mistakes.  On my part, many of the mistakes are mine, but sometimes I find one of theirs.

You can find the code here (click on the icon):

 

First add a media player to the XAML:

And a couple of buttons so that you can see it work.  Shifty

Code Snippet

  1. <Grid>
  2.         <StackPanel Margin="20,240,0,340">
  3.             <TextBlock x:Name="ShowInstructions"
  4.                        Text="Swipe down from top or up from the bottom then press either button"
  5.                        FontSize="36" Height="96" Margin="0,0,44,0" />
  6.             <TextBlock x:Name="ShowButtonPushed"
  7.                        Text="Watch this area to see which button was pressed on the App Bar"
  8.                        FontSize="36" Height="96" Margin="0,0,64,0" />
  9.             <MediaPlayer x:Name="mediaPlayer"
  10.                            Width="400" />
  11.         </StackPanel>
  12.     </Grid>

Now add the C# code

C# code to the code behind

  1. private void btnLaugh(object sender, TappedRoutedEventArgs e)
  2.         {
  3.             /***********************************************************************
  4.              * You do not have to place your media files into the Assets folder
  5.              * I did so to keep things really simple for this blog
  6.              * It usually is better to give sounds a separate folder
  7.              * ********************************************************************/
  8.             mediaPlayer.Source = new Uri(mediaPlayer.BaseUri, "Assets/high giggle.wav");
  9.             ShowButtonPushed.Text = "Last button pushed was laugh";
  10.         }
  11.  
  12.         private void btnSharpBlast(object sender, TappedRoutedEventArgs e)
  13.         {
  14.             /***********************************************************************
  15.              * Code is similar to the previous method
  16.              * If you want to change the name (originally btnSharpBlast) to
  17.              * something else, then you need to change the event
  18.              * name in the XAML as well
  19.              * ********************************************************************/
  20.             mediaPlayer.Source = new Uri(mediaPlayer.BaseUri, "Assets/sharp blast.wav");
  21.             ShowButtonPushed.Text = "Last button pushed was Sharp Blast";
  22.         }