title: Play sound in C# on button press
body: <p>I've tried to play a sound from a button event handler: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Runtime.InteropServices.WindowsRuntime;
  using Windows.Foundation;
  using Windows.Foundation.Collections;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Controls.Primitives;
  using Windows.UI.Xaml.Data;
  using Windows.UI.Xaml.Input;
  using Windows.UI.Xaml.Media;
  using Windows.UI.Xaml.Navigation;
  
  // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&amp;clcid=0x409
  
  namespace App1
  {
      /// &lt;summary&gt;
      /// An empty page that can be used on its own or navigated to within a Frame.
      /// &lt;/summary&gt;
      public sealed partial class MainPage : Page
      {
          public MainPage()
          {
              this.InitializeComponent();
          }
  
          private void mainButton_Click_1(object sender, RoutedEventArgs e)
          {
              System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\mywavfile.wav");
              player.Play();
          }
      }
  }
  </code></pre>
  
  <p>But I'm getting an error where the type or namespace "media" does not exist in the namespace "system". What seems to be the problem? </p>
  
  <p>I'm using visual studio 2017 and I've created a universal windows c# blank app. </p>
  
  <p>Edit: </p>
  
  <p>I've changed my code to this but I'm still not getting any audio playback. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Runtime.InteropServices.WindowsRuntime;
  using Windows.Foundation;
  using Windows.Foundation.Collections;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Controls.Primitives;
  using Windows.UI.Xaml.Data;
  using Windows.UI.Xaml.Input;
  using Windows.UI.Xaml.Media;
  using Windows.UI.Xaml.Navigation;
  
  // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&amp;clcid=0x409
  
  namespace App1
  {
      /// &lt;summary&gt;
      /// An empty page that can be used on its own or navigated to within a Frame.
      /// &lt;/summary&gt;
      public sealed partial class MainPage : Page
      {
          public MainPage()
          {
              this.InitializeComponent();
          }
  
          private void mainButton_Click_1(object sender, RoutedEventArgs e)
          {
              MediaElement PlayMusic = new MediaElement();
              PlayMusic.AudioCategory = Windows.UI.Xaml.Media.AudioCategory.Media;
              PlayMusic.Source = new Uri(@"D:/Downloads/sounds.mp3");
              PlayMusic.Play();
          }
      }
  }
  </code></pre>
  
  <p>I've tried adding a background declaration tho to allow background audio but still to no avail. </p>