-
-
Save TJRoger/65642fea2c6d5682b54536ef8dfd79bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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