From ccffcfd70925cfa1caf4b45af08ca3533aa3957e Mon Sep 17 00:00:00 2001 From: Krzysztof Moch Date: Mon, 8 Jul 2024 13:41:21 +0200 Subject: [PATCH] fix(ios): don't pause playback when entering background (#3973) --- ios/Video/Features/RCTPictureInPicture.swift | 2 +- ios/Video/RCTVideo.swift | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ios/Video/Features/RCTPictureInPicture.swift b/ios/Video/Features/RCTPictureInPicture.swift index 0d745d7c..ab24cd87 100644 --- a/ios/Video/Features/RCTPictureInPicture.swift +++ b/ios/Video/Features/RCTPictureInPicture.swift @@ -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( diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index e8c91d84..7b30c693 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -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