Reputation: 401
I have an AVQueuePlayer that plays audio files from the internet. It is enabled to perform with the app in the background and does that fine (plays in the background and asynchronously fetches new files in the background).
All is fine when I allow the AVQueuePlayer to start playing before moving the app to the background. In order to start playing my app has to talk to a couple of services before it actually gets the correct files to play.
If I move the app to the background in those vital couple of seconds before playback has started then the AVQueuePlayer will not play.
In order to be able to talk to the services in the background I always ask for background task permission first via beginBackgroundTaskWithExpirationHandler.
Watching the logs I can see that all the services complete normally (so the background task permission works) and the AVQueuePlayer is correctly initialised with multiple AVPlayerItems but it will not start playback. AVQueuePlayer.currentItem.status also never gets to AVPlayerStatusReadyToPlay.
I made sure all interaction with the AVQueuePlayer occurs on the main thread.
I am stumped, the AVQueuePlayer will not start if the app is in the background regardless of my settings and me having background task permission.
I have previously been able to do something similar with Audio Queue Services but was hoping that AVQueuePlayer and it's simpler API would suffice this time but it appears I'll have to go back to Audio Queue Services and co.
Upvotes: 2
Views: 516
Reputation: 70673
You have to start the Audio Queue while the app is still in the foreground. Play silence if necessary. It looks like if the app is not producing audio when it goes into the background, it's audio session is taken away and given to the new foreground app by default. Thus the app going into the background has no session in which to play anything.
Upvotes: 2