Im making a windows phone 8.1 app. It sends a playlist to BackgroundMediaPlayer, and player starts playing somgs. But sometimes I get an exception "The RPC server is unavailable" when sending message with BackgroundMediaPlayer.SendMessageToBackground, and the only thing can fix it is restarting app. Im testing it on windows 10 mobile and not sure if this problems exists on WP8.1, but no problems in WP8.1 emulator
I ran into a similar problem working in a Windows 10 Universal project, except I had no successful interaction with BackgroundMediaPlayer at all. The cause was my background audio task was not being started; after ensuring it started I could successfully access BackgroundMediaPlayer.Current member, and use BackgroundMediaPlayer.SendMessageToBackground etc. It was a combination of the official Microsoft sample for Background Audio actually working, and this post by another member that led me to the solution.
It sounds like your initial interaction with the BackgroundMediaPlayer working for a time, but then it stops working. Could it be that it's being shutdown or suspended prematurely? Are you obtaining a deferral in the task's Run method and storing it? Hooking up event handlers for TaskCompleted and OnCancelled after you have obtained the deferral? Is it possible BackgroundMediaPlayer.Shutdown() is being called somewhere accidentally?
I originally wrote this answer before I realised that the OP was playing audio successfully, and facing this problem only after the program had been running awhile. The remainder of my original answer is as follows (it might help someone with a similar problem):
My background task wasn't starting because it was within a Class Library instead of a Windows Runtime Component (This matters, I think, because the process that provides background audio, Windows.Media.BackgroundPlayback.exe, is a native application and must rely on interop to interact with the tasks we write (and it loads our task as part of its process, not part of our foreground app that uses the task); this Microsoft guide does specifically state "Windows Runtime Component" when advising placing all background tasks in a separate project ).
By changing my Class Library to a Windows Runtime Component (which involved making the task class sealed, and the other restrictions entailed), the IBackgroundTask.Run method was invoked, I obtained a deferral, and all my calls to interact with the BackgroundMediaPlayer began working properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.