SRGSSR / srgmediaplayer-apple

An advanced media player library, simple and reliable
MIT License
158 stars 33 forks source link

Video playback stops when locking the device #66

Closed defagos closed 5 years ago

defagos commented 5 years ago

Probably starting with a recent iOS 12 version (yet to be determined), playback stops when on the lock screen, which was not enforced by SRG Media Player.

We should detach the video layer to achieve proper behavior again. Disabling visual tracks does not work for streamed medias and is therefore not a good strategy.

defagos commented 5 years ago

Works fine with iOS 12.2, not anymore with iOS 12.3.1. The change probably was made in iOS 12.3.

defagos commented 5 years ago

What changed in iOS 12.3 is how the system handles the player state when bound to a player layer:

As mentioned above, the correct way to have video playback continue in background is to detach the player layer in background. Note that background includes lock screen playback as well (this is considered background, but was treated differently in earlier iOS versions from an AVPlayer perspective).

I initially intended to provide an API with the following modes:

This API is tightly bound to the iOS behavior, though, and would lead to different implementations based on iOS versions. The system behavior might change in the future as well.

To avoid such issues and provide for a more consistent behavior, I finally decided to provide an API with the following modes:

No strong guarantees are made about those modes (as the result might vary between iOS versions depending on what the system enforces), but the general rule is to detach layers to maximize the chances for background playback. Note that audio playback is never affected and works in background.

Using this new API, we therefore have:

defagos commented 5 years ago

Available for review on the feature/background-playback branch.

defagos commented 5 years ago

What remains to be done:

defagos commented 5 years ago

Answers:

defagos commented 4 years ago

Testing is a bit cumbersome and can only be manual.

The main idea can be summarised as follows: If a view is attached to the player and installed in the view hierarchy, playback will continue or be paused depending on the viewBackgroundBehavior controller setting. If the view is not installed, this setting has no effect and background video playback is always possible.

I updated the inline demo so that playing with all kinds of different setups is easy. Just build the demo and check that the following expected behaviours are fulfilled when playing a video.

Here is the expected result for all possible settings:

View attached to the player Controller view background behavior Video playback continues in background?
Yes Attached No
Yes Detached Yes
Yes Detached when locked Only if the device is locked with the app in front
No Attached Yes
No Detached Yes
No Detached when locked Yes