Skip to content

Instantly share code, notes, and snippets.

@halilozel1903
Created June 13, 2019 09:14
Show Gist options
  • Save halilozel1903/3ec875eff3aef0d8daff1423f055cf8e to your computer and use it in GitHub Desktop.
Save halilozel1903/3ec875eff3aef0d8daff1423f055cf8e 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