@@ -68,10 +68,6 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
6868 return UserDefaults . standard. bool ( forKey: " doubleTapSeekEnabled " )
6969 }
7070
71- private var isPipAutoEnabled : Bool {
72- UserDefaults . standard. bool ( forKey: " pipAutoEnabled " )
73- }
74-
7571 private var isPipButtonVisible : Bool {
7672 if UserDefaults . standard. object ( forKey: " pipButtonVisible " ) == nil {
7773 return true
@@ -484,8 +480,7 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
484480 name: . AVPlayerItemDidPlayToEndTime,
485481 object: player. currentItem
486482 )
487-
488-
483+ NotificationCenter . default. addObserver ( self , selector: #selector( startPipIfNeeded) , name: UIApplication . didEnterBackgroundNotification, object: nil )
489484 }
490485
491486 private func setupTopRowLayout( ) {
@@ -2211,28 +2206,17 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
22112206 }
22122207
22132208 @objc private func pipButtonTapped( _ sender: UIButton ) {
2214- Logger . shared. log ( " PiP button tapped " , type: " Debug " )
2215- guard let pip = pipController else {
2216- Logger . shared. log ( " PiP controller is nil " , type: " Error " )
2217- return
2218- }
2219- Logger . shared. log ( " PiP controller found, isActive: \( pip. isPictureInPictureActive) " , type: " Debug " )
2209+ guard let pip = pipController else { return }
22202210 if pip. isPictureInPictureActive {
22212211 pip. stopPictureInPicture ( )
2222- Logger . shared. log ( " Stopping PiP " , type: " Debug " )
22232212 } else {
22242213 pip. startPictureInPicture ( )
2225- Logger . shared. log ( " Starting PiP " , type: " Debug " )
22262214 }
22272215 }
22282216
22292217 @objc private func startPipIfNeeded( ) {
2230- guard isPipAutoEnabled,
2231- let pip = pipController,
2232- !pip. isPictureInPictureActive else {
2233- return
2234- }
2235- pip. startPictureInPicture ( )
2218+ Logger . shared. log ( " PIP " , type: " Genral " )
2219+ pipController!. startPictureInPicture ( )
22362220 }
22372221
22382222 @objc private func lockTapped( ) {
@@ -3595,12 +3579,12 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
35953579 playerLayerContainer. trailingAnchor. constraint ( equalTo: view. trailingAnchor)
35963580 ] )
35973581
3598- let playerLayer = AVPlayerLayer ( player: player)
3599- playerLayer . frame = playerLayerContainer . bounds
3600- playerLayer . videoGravity = . resizeAspect
3601- playerLayerContainer . layer . addSublayer ( playerLayer )
3602-
3603- pipController = AVPictureInPictureController ( playerLayer: playerLayer )
3582+ let pipPlayerLayer = AVPlayerLayer ( player: playerViewController . player)
3583+ pipPlayerLayer . frame = playerViewController . view . layer . bounds
3584+ pipPlayerLayer . videoGravity = . resizeAspect
3585+
3586+ playerViewController . view . layer . insertSublayer ( pipPlayerLayer , at : 0 )
3587+ pipController = AVPictureInPictureController ( playerLayer: pipPlayerLayer )
36043588 pipController? . delegate = self
36053589
36063590 let Image = UIImage ( systemName: " pip " , withConfiguration: cfg)
@@ -3625,7 +3609,7 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
36253609
36263610 pipButton. isHidden = !isPipButtonVisible
36273611
3628- NotificationCenter . default. addObserver ( self , selector: #selector( startPipIfNeeded) , name: UIApplication . willResignActiveNotification , object: nil )
3612+ NotificationCenter . default. addObserver ( self , selector: #selector( startPipIfNeeded) , name: UIApplication . didEnterBackgroundNotification , object: nil )
36293613 }
36303614
36313615 func updateMarqueeConstraints( ) {
0 commit comments