title: BackgroundMediaPlayer not playing music files outside Music Folder
body: <p>I am having this strange issue. I am trying to create a music player app for <code>Windows Phone 8.1</code>. I need the music player to play the music in background, even if the application is in background/closed state. So I am using the <a href="https://msdn.microsoft.com/en-us/windows.media.playback.backgroundmediaplayer" rel="nofollow noreferrer"><strong>BackgroundMediaPlayer</strong></a> class for playing the music. Following is the code for the same:</p>
  
  <pre><code>private void SendMessageToBackground(string Path, string Name, bool IsRadio)
  {
      if (!string.IsNullOrEmpty(Path))
      {
          var message = new ValueSet();
          message.Add(Constants.TrackURI, Path);
          message.Add(Constants.TrackName, Name);
          message.Add(Constants.IsRadio, IsRadio);
          BackgroundMediaPlayer.MessageReceivedFromBackground += BackgroundMediaPlayer_MessageReceivedFromBackground;
          BackgroundMediaPlayer.Current.MediaFailed += Current_MediaFailed;
          BackgroundMediaPlayer.SendMessageToBackground(message);
      }
  }
  </code></pre>
  
  <p>The code works all fine, the music is playing smoothly. But now I am facing an issue. I am fetching the music files in the application by looping through all folders. Hence it displays all music files, wherever they are in the folder hierarchy. But when I am trying to play a file outside the <code>Music</code> folder created by the windows phone(refer screenshot), the background music player simply doesn't play the file.</p>
  
  <p><a href="https://i.stack.imgur.com/kAP2k.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kAP2k.jpg" alt="Music File Issue"></a></p>
  
  <p>As seen in the code I am listening to events from the <code>BackgroundMediaPlayer</code> and its <code>MediaPlayer</code> to check for any possible failure reasons, but both these events are not getting triggered in case of a failed playback. But the <code>BackgroundMediaPlayer</code>'s <code>MessageReceivedFromBackground</code> event is getting triggered correctly in case of successful playbacks(from Music folder). I checked the file path being passed, and it was all correct, and I tried playing these music files using the inbuilt Music app of the phone, and that too played successfully. What can be the possible problem here? How can I solve it?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I debugged further and figured out that it is actually an permission issue. The path passed from the above code, I was using in another function, to pass the <code>Uri</code>/<code>StorageFile</code> to the <code>BackgroundMediaPlayer.Current</code>. Code:</p>
  
  <pre><code>public async void StartTrackAt(string TrackURI, string Name)
  {
      try
      {
          BackgroundMediaPlayer.Current.SetUriSource(new Uri(TrackURI)); // This works fine in debug mode. Throws Exception in release mode
          /* Tried this also, but this doesn't even work in debug mode
          StorageFile storageFile = await StorageFile.GetFileFromPathAsync(TrackURI);
          mediaPlayer.SetFileSource(s);
          */
      }
      catch (Exception ex)
      {
          //Control comes here when trying to play files outside Music folder.
      }
  }
  </code></pre>
  
  <p>I have set the required capabilities in the app's manifest file. But I got the following <code>Exception</code> when I tried to run this application:</p>
  
  <pre><code>{System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at BackgroundMusicPlayer.MyPlayListManager.&lt;StartTrackAt&gt;d__0.MoveNext()}
      [System.UnauthorizedAccessException]: {System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at BackgroundMusicPlayer.MyPlayListManager.&lt;StartTrackAt&gt;d__0.MoveNext()}
      Data: {System.Collections.ListDictionaryInternal}
      HelpLink: null
      HResult: -2147024891
      InnerException: null
      Message: "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
      Source: "mscorlib"
      StackTrace: "   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at BackgroundMusicPlayer.MyPlayListManager.&lt;StartTrackAt&gt;d__0.MoveNext()"
  </code></pre>
  
  <p>Strangely enough, when passing the <code>Uri</code> directly without fetching the <code>StorageFile</code>, it works well in debug mode.</p>
  
  <p>Here are the capabilities I have set for the application:</p>
  
  <p><a href="https://i.stack.imgur.com/SbGWs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SbGWs.png" alt="App Capabilities"></a></p>