Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video playback stops when locking the device #66

Closed
defagos opened this issue Jun 23, 2019 · 6 comments
Closed

Video playback stops when locking the device #66

defagos opened this issue Jun 23, 2019 · 6 comments
Milestone

Comments

@defagos
Copy link
Member

defagos commented Jun 23, 2019

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
Copy link
Member Author

defagos commented Jun 24, 2019

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

@defagos
Copy link
Member Author

defagos commented Jun 24, 2019

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

  • iOS 12.2 and below: If a player is attached to a player layer and the app is sent to the background while playing a video, the player is paused. No pause occurs if the device is simply locked.
  • iOS 12.3 and above: If a player is attached to a player layer and the app is sent to the background or the device locked when playing a video, the player is paused.

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:

  • Background playback.
  • Background playback on the lock screen only.
  • No background playback.

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:

  • Layer kept attached in background.
  • Layer detached in background.
  • Layer detached in background, but only if the device is locked.

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:

  • Prior to iOS 12.3:
    • Kept attached / detached when locked: Video playback is paused in background, but continues if the device is locked.
    • Detached: Video playback continues in background and if the device is locked.
  • iOS 12.3 and above:
    • Kept attached: Video playback is paused in background and if the device is locked.
    • Detached: Video playback continues in background and if the device is locked.
    • Detached when locked: Video playback is paused in background, but continues if the device is locked.

@defagos
Copy link
Member Author

defagos commented Jun 24, 2019

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

@defagos
Copy link
Member Author

defagos commented Jun 24, 2019

What remains to be done:

  • Investigate if for streams with audio-only the detached player only plays the audio.
  • Reattach the view just earlier, so that the layer is immediately visible when returning to foreground.

@defagos
Copy link
Member Author

defagos commented Jun 24, 2019

Answers:

  • Using Apple basic stream (which has a single audio variant), it appears the player does not switch to this variant when detached from the layer.
  • Done, see 1906a32.

@defagos
Copy link
Member Author

defagos commented May 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant