title: How to play background music in windows phone 8.1 with xaml and c#?
body: <p>I've searched a lot, through msdn site, stackoverflow etc.. but i can't make playing a song in background. It stops when change frame, or exiting from the app returning to start, it also starts randomly when it is on lockscreen or in another places.. Can you explain how correctly do this code please? Sorry for bad code but i've started few days ago.</p>
  
  <p>this is more or less what i found on microsoft site. There are also no system it doesn't show any control</p>
  
  <pre><code>public sealed partial class listView : Page
  {
       SystemMediaTransportControls Player = SystemMediaTransportControls.GetForCurrentView();
  
  
      SystemMediaTransportControls systemControls;
      MediaElement musicPlayer = new MediaElement();
  
  
      public listView()
      {
  
          this.InitializeComponent();
          makeSongList();
  
          // Per usare il tasto back
          HardwareButtons.BackPressed += HardwareButtons_BackPressed;
  
          // Hook up app to system transport controls.
          systemControls = SystemMediaTransportControls.GetForCurrentView();
          systemControls.ButtonPressed += SystemControls_ButtonPressed;
  
          // Register to handle the following system transpot control buttons.
          systemControls.IsEnabled = true;
          systemControls.IsPlayEnabled = true;
          systemControls.IsPauseEnabled = true;
          systemControls.IsNextEnabled = true;
          systemControls.IsPreviousEnabled = true;
  
          musicPlayer.AudioCategory = AudioCategory.BackgroundCapableMedia;
  
  
  
  
      }
  
  
      void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
      {
          // gestisce l'evento CurrentStateChanged di MediaElement e
          // aggiorna la proprietà PlaybackStatus di SystemMediaTransportControls.
  
          switch (musicPlayer.CurrentState)
          {
              case MediaElementState.Playing:
                  systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
                  break;
              case MediaElementState.Paused:
                  systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
                  break;
              case MediaElementState.Stopped:
                  systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
                  break;
              case MediaElementState.Closed:
                  systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
                  break;
              default:
                  break;
          }
      }
  
      async private void UpdateSongInfo()
      {
          // Get the updater.
          SystemMediaTransportControlsDisplayUpdater updater = systemControls.DisplayUpdater;
  
          // Get the music file and pass it to CopyFromFileAsync to extract the metadata
          // and thumbnail. StorageFile is defined in Windows.Storage
          StorageFile musicFile =
              await StorageFile.GetFileFromApplicationUriAsync(new Uri(currentPath));
  
          await updater.CopyFromFileAsync(MediaPlaybackType.Music, musicFile);
  
          // Update the system media transport controls
          updater.Update();
      }
  
      void MusicPlayer_MediaOpened(object sender, RoutedEventArgs e)
      {
          UpdateSongInfo();
      }
  
  
      void SystemControls_ButtonPressed(SystemMediaTransportControls sender,
                                        SystemMediaTransportControlsButtonPressedEventArgs args)
      {
          switch (args.Button)
          {
              case SystemMediaTransportControlsButton.Play:
                  PlayMedia();
                  break;
              case SystemMediaTransportControlsButton.Pause:
                  PauseMedia();
                  break;
              default:
                  break;
          }
      }
  
      async void PlayMedia()
      {
          await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;
          {
              musicPlayer.Play();
          });
      }
  
      async void PauseMedia()
      {
          await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;
          {
              musicPlayer.Pause();
          });
      }
  </code></pre>