2

I'm using AVPlayerViewController to play a local video inline.

My desired behavior is that videos playing in a PIP window will close automatically when the video ends.

Is there a way to do this with AVPlayerViewController?

Edit: Also posted on the Apple Developer Forums https://developer.apple.com/forums/thread/652748

import UIKit
import AVKit

class ViewController: UIViewController {
    var playerViewController = AVPlayerViewController()
    var videoPath = "video.mov"

    override func viewDidLoad() {
        super.viewDidLoad()
        
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
        } catch { }
            
        let player = AVPlayer(url: URL(fileURLWithPath: videoPath))
        playerViewController.player = player
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(playerDidFinishPlaying),
            name: .AVPlayerItemDidPlayToEndTime,
            object: player.currentItem
        )
            
        playerViewController.showsPlaybackControls = true
        playerViewController.entersFullScreenWhenPlaybackBegins = true
        self.addChild(playerViewController)
        self.view.addSubview(playerViewController.view)
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification) {
         // ???
    }
}
Jody K
  • 43
  • 6
  • [Senthil](https://stackoverflow.com/users/16649284) posted an [Answer](https://stackoverflow.com/a/68755962) saying "Have you tried calling stopPictureInPicture method to stop the existing PIP playback?" – Scratte Aug 19 '21 at 09:02

0 Answers0