Create a Music Player app in C#

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:

  • Pause: Pauses media at the current position.
  • Play: Plays media from the current position.
  • Stop: Stops and resets media to be played from the beginning.

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.   
  15. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238   
  16.   
  17. namespace Music_Player   
  18. {   
  19.     /// <summary>   
  20.     /// An empty page that can be used on its own or navigated to within a Frame.   
  21.     /// </summary>   
  22.     public sealed partial class MainPage : Page   
  23.     {   
  24.         public MainPage()   
  25.         {   
  26.             this.InitializeComponent();   
  27.         }   
  28.   
  29.         /// <summary>   
  30.         /// Invoked when this page is about to be displayed in a Frame.   
  31.         /// </summary>   
  32.         /// <param name="e">Event data that describes how this page was reached.  The Parameter   
  33.         /// property is typically used to configure the page.</param>   
  34.         protected override void OnNavigatedTo(NavigationEventArgs e)   
  35.         {   
  36.         }   
  37.   
  38.         private void Button_Click_1(object sender, RoutedEventArgs e)   
  39.         {   
  40.             Media.Play();   
  41.         }   
  42.   
  43.         private void Button_Click_2(object sender, RoutedEventArgs e)   
  44.         {   
  45.             Media.Pause();   
  46.         }   
  47.   
  48.         private void Button_Click_3(object sender, RoutedEventArgs e)   
  49.         {   
  50.             Media.Stop();   
  51.         }   
  52.   
  53.         private void Button_Click_4(object sender, RoutedEventArgs e)   
  54.         {   
  55.             if (Media.IsMuted)   
  56.             {   
  57.                 Media.IsMuted = false;   
  58.             }   
  59.   
  60.             if (Media.Volume > 0)   
  61.             {   
  62.                 Media.Volume -= .1;   
  63.             }   
  64.         }   
  65.   
  66.         private void Button_Click_5(object sender, RoutedEventArgs e)   
  67.         {   
  68.             if (Media.IsMuted)   
  69.             {   
  70.                 Media.IsMuted = false;   
  71.             }   
  72.   
  73.             if (Media.Volume < 1)   
  74.             {   
  75.                 Media.Volume += .1;   
  76.             }   
  77.         }   
  78.     }   
  79. }