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.
Code Snippet
- <Grid>
- <StackPanel Margin="20,240,0,340">
- <TextBlock x:Name="ShowInstructions"
- Text="Swipe down from top or up from the bottom then press either button"
- FontSize="36" Height="96" Margin="0,0,44,0" />
- <TextBlock x:Name="ShowButtonPushed"
- Text="Watch this area to see which button was pressed on the App Bar"
- FontSize="36" Height="96" Margin="0,0,64,0" />
- <MediaPlayer x:Name="mediaPlayer"
- Width="400" />
- </StackPanel>
- </Grid>
Now add the C# code
C# code to the code behind
- private void btnLaugh(object sender, TappedRoutedEventArgs e)
- {
- /***********************************************************************
- * You do not have to place your media files into the Assets folder
- * I did so to keep things really simple for this blog
- * It usually is better to give sounds a separate folder
- * ********************************************************************/
- mediaPlayer.Source = new Uri(mediaPlayer.BaseUri, "Assets/high giggle.wav");
- ShowButtonPushed.Text = "Last button pushed was laugh";
- }
- private void btnSharpBlast(object sender, TappedRoutedEventArgs e)
- {
- /***********************************************************************
- * Code is similar to the previous method
- * If you want to change the name (originally btnSharpBlast) to
- * something else, then you need to change the event
- * name in the XAML as well
- * ********************************************************************/
- mediaPlayer.Source = new Uri(mediaPlayer.BaseUri, "Assets/sharp blast.wav");
- ShowButtonPushed.Text = "Last button pushed was Sharp Blast";
- }