fix(ios): don't pause playback when entering background (#3973)

This commit is contained in:
Krzysztof Moch 2024-07-08 13:41:21 +02:00 committed by GitHub
parent a3ecc0108c
commit ccffcfd709
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -6,11 +6,11 @@ import React
#if os(iOS)
class RCTPictureInPicture: NSObject, AVPictureInPictureControllerDelegate {
public private(set) var _pipController: AVPictureInPictureController?
private var _onPictureInPictureEnter: (() -> Void)?
private var _onPictureInPictureExit: (() -> Void)?
private var _onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)?
private var _restoreUserInterfaceForPIPStopCompletionHandler: ((Bool) -> Void)?
private var _pipController: AVPictureInPictureController?
private var _isActive = false
init(

View File

@ -156,6 +156,15 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
func handlePictureInPictureExit() {
onPictureInPictureStatusChanged?(["isActive": NSNumber(value: false)])
// To continue audio playback in backgroud we need to set
// player in _playerLayer & _playerViewController to nil
let appState = UIApplication.shared.applicationState
if _playInBackground && appState == .background {
_playerLayer?.player = nil
_playerViewController?.player = nil
_player?.play()
}
}
func handleRestoreUserInterfaceForPictureInPictureStop() {
@ -293,7 +302,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc
func applicationDidEnterBackground(notification _: NSNotification!) {
let isExternalPlaybackActive = _player?.isExternalPlaybackActive ?? false
if _playInBackground || isExternalPlaybackActive { return }
if !_playInBackground || isExternalPlaybackActive || _pip?._pipController?.isPictureInPictureActive == true { return }
// Needed to play sound in background. See https://developer.apple.com/library/ios/qa/qa1668/_index.html
_playerLayer?.player = nil
_playerViewController?.player = nil