iOS 14 regression or documentation issue: Video playback is incorrectly paused when moving to the background if implemented according to QA1668

Originator:defagos
Number:rdar://FB8722590 Date Originated:Sep 21, 2020
Status:Open Resolved:
Product:iOS Product Version:14
Classification:Incorrect/Unexpected Behavior Reproducible:Always
 
Description of the problem:

By its very nature video playback is usually supposed to happen when the application is in the foreground. Still it has always been possible to let video playback seamlessly continue when the application is sent to the background. As described in the official QA1668 article (https://developer.apple.com/library/archive/qa/qa1668/_index.html) this is achieved by detaching the player layer from its player when entering background. This avoids the player layer pausing playback, yielding the expected behavior.

As of iOS 14, though, playback is paused when entering background, even if the player layer has been correctly detached in response to `UIApplication.didEnterBackgroundNotification` (see QA1668 reference implementation). Playback can be resumed in the background if the app implements control center support, but the end user experience is poor.

Note that playback is not paused if the player layer was detached before or never attached in the first place, which leads me to think there is a subtle timing issue which changed this documented behavior in iOS 14. If you detach the player layer in `UIApplication.willResignActiveNotification` video playback namely continues as expected in the background, which incidentally provides a potential workaround or official solution to this issue.

Such changes could affect apps in other subtle ways, which is why I reported this issue so that your teams can determine whether this change is a regression or not.

If this behavior is expected starting with iOS 14 and is not a regression, it should at least be documented correctly and the QA1668 article mentioned above should be updated, maybe to mention that `UIApplication.willResignActiveNotification` is now the way to go.

How to reproduce the issue:

I attached a sample project to this issue, whose implementation is based on QA1668. This project compiles with Xcode 11.7 / iOS 13.7 SDK as well as Xcode 12 / iOS 14 SDK.

1. Open the sample project and set a development team for deployment on a physical device.
2. Run the project on an iOS device.
3. Tap on the play button. This plays a video.
4. Send the application to the background.

Here is the result you obtain, which is different between iOS versions:

- On iOS 14 (project built with the iOS 14 SDK or iOS 13.7 SDK), video playback is incorrectly paused when moving to the background.
- On iOS 13 (project built with the iOS 14 SDK or iOS 13.7 SDK), video playback correctly continues when moving to the background.

Two additional variants mentioned above can be tested with this project:

- If you comment occurrences of `playerLayer.player = player` in the sample code (i.e. the player layer is never attached) and run the project on iOS 14, the behavior is correct and video playback continues while moving the app to the background.
- If you replace `UIApplication.didEnterBackgroundNotification ` with `UIApplication.willResignActiveNotification` video playback continues while moving the app to the background.

Possible solutions:

- The new behavior might be desired, in which case QA1668 should be updated to mention this iOS 14 change.
- The new behavior is a regression, QA1668 is still valid and the regression should be fixed.

Expected behavior:

If an `AVPlayer` is playing a video and its video layer is detached when moving to the background according to QA1668 (`UIApplication.didEnterBackgroundNotification`), playback continues, no matter the iOS version.

Actual behavior:

If an `AVPlayer` is playing a video and its video layer is detached when moving to the background according to QA1668 (`UIApplication.didEnterBackgroundNotification`), playback is incorrectly paused starting with iOS 14.

Comments

Sample code

Available on GitHub: https://github.com/defagos/radars/tree/master/background-video-playback


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!