Skip to content

Instantly share code, notes, and snippets.

@TJRoger
Forked from halilozel1903/pip2.swift
Created March 17, 2021 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TJRoger/65642fea2c6d5682b54536ef8dfd79bc to your computer and use it in GitHub Desktop.
Save TJRoger/65642fea2c6d5682b54536ef8dfd79bc to your computer and use it in GitHub Desktop.
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController, AVPlayerViewControllerDelegate {
var playerController : AVPlayerViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func play(_ sender: Any) {
let path = Bundle.main.path(forResource: "Apple", ofType: "mp4")!
let url = NSURL(fileURLWithPath: path)
let player = AVPlayer(url: url as URL)
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)
}
@objc func didfinishPlaying(note : NSNotification) {
playerController.dismiss(animated: true, completion: nil)
let alertView = UIAlertController(title: "Finished", message: "Video finished", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "Okey", style: .default, handler: nil))
self.present(alertView, animated: true, completion: nil)
}
func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
let currentviewController = navigationController?.visibleViewController
if currentviewController != playerViewController{
currentviewController?.present(playerViewController, animated: true, completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment