Introduction

Windows 8 Apps have a good control for playing audio & videos in applications. I have integrated a Media Control in this sample and used several methods to make it work.

To embed a music or video player in your application we use the “MediaElement class”. I also created customized controls to give more functionality such as play, pause, volume +, Volume – and Stop.

You can use also “enable full-screen mode” when you use a video. I used the MediaElement class to control and manage audio media playback in a Windows Store app using C#.

In this sample the MediaElement class has the following methods:

The MediaElement class has these properties:

IsMuted Gets or sets a value indicating whether the audio is muted.

Volume Gets or sets the media’s volume.

Description

I’ve used Visual Studio Ultimate 2012 and used the following 5 buttons:

Button Click handler action
Play Calls the Play method.
Pause Calls the Pause method.
Stop Calls the Stop method.
Vol – Volume Down
Vol + Volume Up

And one MediaElement (Name property: Media).

C# Code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Windows.Foundation;
  6. using Windows.Foundation.Collections;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Controls.Primitives;
  10. using Windows.UI.Xaml.Data;
  11. using Windows.UI.Xaml.Input;
  12. using Windows.UI.Xaml.Media;
  13. using Windows.UI.Xaml.Navigation;
  14. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  15. namespace Music_Player
  16. {
  17. /// <summary>
  18. /// An empty page that can be used on its own or navigated to within a Frame.
  19. /// </summary>
  20. public sealed partial class MainPage : Page
  21. {
  22. public MainPage()
  23. {
  24. this.InitializeComponent();
  25. }
  26. /// <summary>
  27. /// Invoked when this page is about to be displayed in a Frame.
  28. /// </summary>
  29. /// <param name="e">Event data that describes how this page was reached. The Parameter
  30. /// property is typically used to configure the page.</param>
  31. protected override void OnNavigatedTo(NavigationEventArgs e)
  32. {
  33. }
  34. private void Button_Click_1(object sender, RoutedEventArgs e)
  35. {
  36. Media.Play();
  37. }
  38. private void Button_Click_2(object sender, RoutedEventArgs e)
  39. {
  40. Media.Pause();
  41. }
  42. private void Button_Click_3(object sender, RoutedEventArgs e)
  43. {
  44. Media.Stop();
  45. }
  46. private void Button_Click_4(object sender, RoutedEventArgs e)
  47. {
  48. if (Media.IsMuted)
  49. {
  50. Media.IsMuted = false;
  51. }
  52. if (Media.Volume > 0)
  53. {
  54. Media.Volume -= .1;
  55. }
  56. }
  57. private void Button_Click_5(object sender, RoutedEventArgs e)
  58. {
  59. if (Media.IsMuted)
  60. {
  61. Media.IsMuted = false;
  62. }
  63. if (Media.Volume < 1)
  64. {
  65. Media.Volume += .1;
  66. }
  67. }
  68. }
  69. }