iosswiftavplayeravplayerviewcontrollerpicture-in-picture

Not able to put back picture in picture content to its original size


I have a button which presents AVPlayerViewController and i managed to add picture in pciture to my AVPlayerViewController so i can minimize it, now once minimized i want to maximize it again but as soon as i do it the AVPlayerViewController disappears. I don't plan to use storyboard and want to do it programmatically as embeding my view controller in navigation controller resolves the issue but i don't want to follow that approach

I added BackgroundMode capability and ticked PIP and in Appdelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(.playback)
            try audioSession.setActive(true, options: [])
        } catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
        
        return true
    }

and in my ViewController

class ViewController: UIViewController,AVPlayerViewControllerDelegate {
    
    var playerController : AVPlayerViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    
    @IBAction func btnClicked(_ sender: Any) {


        if let videoURL = URL(string: "https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4") {
            let player = AVPlayer(url: videoURL)
            
            playerController = AVPlayerViewController()
            
            NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didfinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
            
            playerController.player = player
            
            playerController.allowsPictureInPicturePlayback = true
            
            playerController.delegate = self
            
            playerController.player?.play()
            
            self.present(playerController, animated: true, completion : nil)
            
        }
    }
    
    
  
    
}

Just for creating a simple demo i used storyboard, in my real app i don't use it


Solution

  • import UIKit
    import AVKit
    import AVFoundation
    
    
    class ViewController: UIViewController,AVPlayerViewControllerDelegate {
        
        let playerController  = AVPlayerViewController()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            playerController.delegate = self
        }
        
        
        @IBAction func btnClicked(_ sender: Any) {
            
            
            if let videoURL = URL(string: "https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4") {
                let player = AVPlayer(url: videoURL)
                
                playerController.player = player
                
                playerController.allowsPictureInPicturePlayback = true
                
                playerController.delegate = self
                
                playerController.player?.play()
                
                self.present(playerController, animated: true, completion : nil)
                
            }
        }
        
        func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
            
            self.present(playerViewController, animated: true) {
                completionHandler(true)
            }
            
        }
        
    }
    

    The above code works without NavigationController