Search code examples
windows-phonemedia-playerwindows-phone-8.1audio-playerbackground-music

Do not play songs from phone music library on device in WP8.1


I develop the music application for Windows Phone 8.1. I use BackgroundTask and access to phone music library. Application work fine on emulator with virtual sd-card, but do not work on device. When trying playback on device, an error occur:

Failed with error code System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

This problem code, which errors occured. Code placed in foreground app for simplycity:

  BackgroundMediaPlayer.Current.SetUriSource(new Uri("C:\\Data\\Users\\Public\\Music\\Ballroom\\Juan Camus - Now That The Love's Gone.mp3"));
        BackgroundMediaPlayer.Current.Play();

Does anyone know how to solve this problem?


Solution

  • Using SetUriSource from the background task does not work when pointing to files that are not packaged with the app. If you want to play files from the user's library or SD card, you need to use SetFileSource or SetStreamSource instead. You can get the file from a path by calling StorageFile.GetFileFromPathAsync(path).

    Unfortunately, this is not documented, so a lot of people will probably run into this (especially since the sample uses SetUriSource). What makes it even worse is the fact that using SetUriSource does work in the simulator and even on the phone, when deploying from VS. But it stops working when submitting to the store. So people will find out very late that their app does not work...