Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import UIKit
  2. import AVFoundation
  3. import AVKit
  4.  
  5. class ViewController: UIViewController, AVPlayerViewControllerDelegate {
  6.  
  7. var playerController : AVPlayerViewController!
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. // Do any additional setup after loading the view.
  12. }
  13.  
  14. override func didReceiveMemoryWarning() {
  15.  
  16. super.didReceiveMemoryWarning()
  17. }
  18.  
  19. @IBAction func play(_ sender: Any) {
  20.  
  21. let path = Bundle.main.path(forResource: "Apple", ofType: "mp4")!
  22.  
  23. let url = NSURL(fileURLWithPath: path)
  24.  
  25. let player = AVPlayer(url: url as URL)
  26.  
  27. playerController = AVPlayerViewController()
  28.  
  29. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didfinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
  30.  
  31. playerController.player = player
  32.  
  33. playerController.allowsPictureInPicturePlayback = true
  34.  
  35. playerController.delegate = self
  36.  
  37. playerController.player?.play()
  38.  
  39. self.present(playerController, animated: true, completion : nil)
  40.  
  41.  
  42. }
  43.  
  44. @objc func didfinishPlaying(note : NSNotification) {
  45.  
  46. playerController.dismiss(animated: true, completion: nil)
  47. let alertView = UIAlertController(title: "Finished", message: "Video finished", preferredStyle: .alert)
  48. alertView.addAction(UIAlertAction(title: "Okey", style: .default, handler: nil))
  49. self.present(alertView, animated: true, completion: nil)
  50. }
  51.  
  52.  
  53. func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
  54.  
  55. let currentviewController = navigationController?.visibleViewController
  56.  
  57. if currentviewController != playerViewController{
  58.  
  59. currentviewController?.present(playerViewController, animated: true, completion: nil)
  60.  
  61. }
  62.  
  63. }
  64.  
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement