none
Why not play songs from phone music library on device in WP8.1? (Wherein music plays in emulator) RRS feed

  • Question

  • 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 the foreground of 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?


    flanker

    Monday, November 10, 2014 2:52 AM

Answers

  • Files in the music library cannot be accessed reliably through a Uri path. Try using SetFileSource instead of SetUriSource. SetFileSource requires a StorageFile object for the music file to be played. Start at KnownFolders.MusicLibrary and use the StorageFolder/StorageFile API to navigate through the file system and obtain a StorageFile object for the desired file.
    • Marked as answer by Boris Salimov Tuesday, November 11, 2014 6:40 AM
    Monday, November 10, 2014 5:46 AM

All replies

  • Files in the music library cannot be accessed reliably through a Uri path. Try using SetFileSource instead of SetUriSource. SetFileSource requires a StorageFile object for the music file to be played. Start at KnownFolders.MusicLibrary and use the StorageFolder/StorageFile API to navigate through the file system and obtain a StorageFile object for the desired file.
    • Marked as answer by Boris Salimov Tuesday, November 11, 2014 6:40 AM
    Monday, November 10, 2014 5:46 AM
  • Thank you! Unfortunately another error ocurs. I was change the code:

      StorageFile file = await StorageFile.GetFileFromPathAsync("C:\\Data\\Users\\Public\\Music\\Ballroom\\Juan Camus - Now That The Love's Gone.mp3");
      BackgroundMediaPlayer.Current.SetFileSource(file);
      BackgroundMediaPlayer.Current.Play();

    When method SetFileSource calling exeption occurs:

    'System.InvalidCastException'
    Unable to cast object of type 'Windows.Media.Playback.MediaPlayer' to type 'Windows.Media.Playback.IMediaPlayerSource'.
    
       at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
       at Windows.Media.Playback.MediaPlayer.SetFileSource(IStorageFile file)
       at MyTestPlayer.MainPage.<CircleButton_Click>d__14.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)
       at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
    Do you  know the cause of this error?


    • Edited by Boris Salimov Monday, November 10, 2014 11:48 AM wrong reply
    Monday, November 10, 2014 11:44 AM
  • Please try doing exactly what I described. Obtain the StorageFile object by starting at KnownFolders.MusicLibrary and navigate through the folder hierarchy to the desired file. I have verified that this works. The media library is a subsystem that actively manages its files. Trying to use a direct file path may not work.

    Something like this:

    StorageFolder ballroomFolder = await KnownFolders.MusicLibrary.GetFolderAsync("Ballroom");
    StorageFile musicFile = await ballroomFolder.GetFileAsync("Juan Camus - Now That The Love's Gone.mp3");
    BackgroundMediaPlayer.Current.SetFileSource(musicFile);

    Monday, November 10, 2014 8:52 PM
  • Thank you! Code that you answered in first answer has been work in background task! (Do not work in foreground, I Think that the Microsoft bug..)

    flanker

    Tuesday, November 11, 2014 6:40 AM
  • Are there plans to get this fixed, or is it fixed with Windows 10 for mobile?  Without this, it basically renders controlling the BackgroundMediaPlayer useless from the foreground process.

    The workaround is to communicate with the background process when the foreground process is running (of course), but that can result in a laggy UX.

    Thanks!

    Saturday, October 3, 2015 6:10 PM