I have run into a perplexing problem with a video streaming app I develop. It is a UWP/PWA (JavaScript) app running web content and using an HTML5 video player with a streaming manager we get from a 3rd party. Every single time the application is put into the background (i.e. the user navigates away to the Xbox home screen), an error occurs that seems related to the streaming manager calling an Edge media extension method of some sort after it has entered a suspended state. As part of our process for when the backgrounding event occurs, we call an asynchronous method that stops video playback.
After some research, I discovered a permission for the app that I felt optimistic about. Given my suspicion that the problem relates to the async stop method not completing quickly enough, I thought checking off "Background Media Playback" in the app manifest might resolve the issue. After all, the description purports to allow media to continue operating even after backgrounding:
The backgroundMediaPlayback capability changes the behavior of the
media-specific APIs like the MediaPlayer and AudioGraph classes to
enable media playback while your app is in the background. All active
audio streams will no longer mute, but will continue to be audible
when an app transitions to the background. Additionally, app lifetime
will be extended automatically while playback is occurring.
https://docs.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations
Sure enough, the error went away after applying this permission to the app. However, I soon noticed it had side effects.
The first side effect was that I could hear audio continuing even after putting the app into the background, but I expected this since the permission is most typically used for apps like Pandora where continued audio playback is a desired feature. Easy fix to mute the player prior to calling its stop method.
The second side-effect turned out to be a blocker. I noticed that reliably, after about 6 seconds in the background, the Xbox always terminates the app. I was perplexed thinking I had introduced a runtime error somehow, but I could not detect any even with Visual Studio debugger attached. After some further research, I found out there are some limitations on the permission I used.
- The app cannot use more than 125 MB of memory
- Media playback must be allowed to continue
What seems to happen is that once the app stops playback, the Xbox determines that it is time to terminate the app instead of putting it into a suspended state as it would normally do.
I cannot seem to figure out a way around this problem. To resolve this error, I need to somehow give the async stop method enough time to do what it needs to do, but I wouldn't want to sacrifice the ability of the app to enter a suspended state.
It would be nice to somehow tell the platform to hold off a little on suspending the app until the async operation completes, but everything else I have tried (ex: requesting extended execution) has been a crapshoot. What I need to do seems simple enough, but this bug has proven infuriatingly stubborn to squash.
question from:
https://stackoverflow.com/questions/65853809/xbox-app-not-allowed-enough-time-to-halt-video-player-when-backgrounded